#from matplotlib.ticker import FuncFormatter
#import pylab as plt
import sys
sys.path.insert(0, '/Users/bolin/NEO/Follow_up/APO_observing')
from apo_observing_functions import *
import matplotlib.pyplot as plt
#matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
import string
import os
import sys
import numpy as np
import random
from decimal import *
import warnings
import re
#import pylab
import math
from matplotlib.widgets import Slider
from bisect import bisect_left
from bisect import bisect_right
import matplotlib.cm as cm
import matplotlib.colors as colors
from matplotlib.ticker import FuncFormatter
from scipy import stats
from sklearn.neighbors import KernelDensity
from statsmodels.nonparametric.kde import KDEUnivariate
from statsmodels.nonparametric.kernel_density import KDEMultivariate
import pyslalib.slalib as sla
import subprocess
import string
from math import log10, exp, sin, tan, sqrt, radians
import pyslalib.slalib as sla
import itertools
from astropy.time import Time
import matplotlib.pyplot as plt
plt.ion()
from scipy.interpolate import Akima1DInterpolator
from scipy.interpolate import interp1d
from apo_observing_functions import *
import zscale as z
from scipy.signal import savgol_filter
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
                               AutoMinorLocator)

sed_paretheses =  'awk -F "[()]" \'{ for (i=2; i<NF; i+=2) print $i }\''

hours_to_deg = 15.0
years_to_months  = 12.
years_to_days = 365.
days_to_hours = 24.
hours_to_minutes = 60.
minutes_to_seconds = 60.
deg_to_arcsec = 3600.
djcal_precision = 5
astrores_seconds_rounding_format_RA = 2
astrores_seconds_rounding_format_Dec = 1
number_array_entries_for_hms = 3
number_coordinates = 2 #1 for RA and 1 for Dec
pipe = '|'
colon = ':'
epoch_cfht = '2000.0'
epoch_2000 = 2000.0
j_2000_mjd = '51544.5'
one = '1'
plus = '+'
minus = '-'
zero = '0'
blank = ''
space = ' '
dot = '.'
underscore = '_'
newline = '\n'
object_name_max_length_cfht = 20
RA_s_length_decimal = 3#string length for astrores formating
Jy_to_mJy = 1000.0
numbered_asteroid_string_max_length = 5
UT_to_HST = (10.0 / days_to_hours)
G = 6.67408e-11 #m^3 kg e^-1 s^-2
mass_sun_kg = 1.989e30
au_to_meters = 149597870700.0
seconds_to_days = 1.0 / (24.*3600.)

visir_constant = 36.0

#plotting variables
fontsize_standard = 14
cbar_label_pad = 17
plt.rcParams['axes.linewidth'] = 1
plt.rcParams['lines.linewidth'] = 1
plt.rcParams['patch.linewidth'] = 1
plt.rcParams['contour.negative_linestyle'] = 'solid'

font = {'weight' : 'bold',
        'size'   : fontsize_standard}
plt.rc('font', **font)
plt.rc('text', usetex=True)



#parula map
from matplotlib.colors import LinearSegmentedColormap

cm_data = [[0.2081, 0.1663, 0.5292], [0.2116238095, 0.1897809524, 0.5776761905],
 [0.212252381, 0.2137714286, 0.6269714286], [0.2081, 0.2386, 0.6770857143],
 [0.1959047619, 0.2644571429, 0.7279], [0.1707285714, 0.2919380952,
  0.779247619], [0.1252714286, 0.3242428571, 0.8302714286],
 [0.0591333333, 0.3598333333, 0.8683333333], [0.0116952381, 0.3875095238,
  0.8819571429], [0.0059571429, 0.4086142857, 0.8828428571],
 [0.0165142857, 0.4266, 0.8786333333], [0.032852381, 0.4430428571,
  0.8719571429], [0.0498142857, 0.4585714286, 0.8640571429],
 [0.0629333333, 0.4736904762, 0.8554380952], [0.0722666667, 0.4886666667,
  0.8467], [0.0779428571, 0.5039857143, 0.8383714286],
 [0.079347619, 0.5200238095, 0.8311809524], [0.0749428571, 0.5375428571,
  0.8262714286], [0.0640571429, 0.5569857143, 0.8239571429],
 [0.0487714286, 0.5772238095, 0.8228285714], [0.0343428571, 0.5965809524,
  0.819852381], [0.0265, 0.6137, 0.8135], [0.0238904762, 0.6286619048,
  0.8037619048], [0.0230904762, 0.6417857143, 0.7912666667],
 [0.0227714286, 0.6534857143, 0.7767571429], [0.0266619048, 0.6641952381,
  0.7607190476], [0.0383714286, 0.6742714286, 0.743552381],
 [0.0589714286, 0.6837571429, 0.7253857143],
 [0.0843, 0.6928333333, 0.7061666667], [0.1132952381, 0.7015, 0.6858571429],
 [0.1452714286, 0.7097571429, 0.6646285714], [0.1801333333, 0.7176571429,
  0.6424333333], [0.2178285714, 0.7250428571, 0.6192619048],
 [0.2586428571, 0.7317142857, 0.5954285714], [0.3021714286, 0.7376047619,
  0.5711857143], [0.3481666667, 0.7424333333, 0.5472666667],
 [0.3952571429, 0.7459, 0.5244428571], [0.4420095238, 0.7480809524,
  0.5033142857], [0.4871238095, 0.7490619048, 0.4839761905],
 [0.5300285714, 0.7491142857, 0.4661142857], [0.5708571429, 0.7485190476,
  0.4493904762], [0.609852381, 0.7473142857, 0.4336857143],
 [0.6473, 0.7456, 0.4188], [0.6834190476, 0.7434761905, 0.4044333333],
 [0.7184095238, 0.7411333333, 0.3904761905],
 [0.7524857143, 0.7384, 0.3768142857], [0.7858428571, 0.7355666667,
  0.3632714286], [0.8185047619, 0.7327333333, 0.3497904762],
 [0.8506571429, 0.7299, 0.3360285714], [0.8824333333, 0.7274333333, 0.3217],
 [0.9139333333, 0.7257857143, 0.3062761905], [0.9449571429, 0.7261142857,
  0.2886428571], [0.9738952381, 0.7313952381, 0.266647619],
 [0.9937714286, 0.7454571429, 0.240347619], [0.9990428571, 0.7653142857,
  0.2164142857], [0.9955333333, 0.7860571429, 0.196652381],
 [0.988, 0.8066, 0.1793666667], [0.9788571429, 0.8271428571, 0.1633142857],
 [0.9697, 0.8481380952, 0.147452381], [0.9625857143, 0.8705142857, 0.1309],
 [0.9588714286, 0.8949, 0.1132428571], [0.9598238095, 0.9218333333,
  0.0948380952], [0.9661, 0.9514428571, 0.0755333333],
 [0.9763, 0.9831, 0.0538]]

parula_map = LinearSegmentedColormap.from_list('parula', cm_data)



#rebound stuff
'''
#slice plot
import rebound
import datetime
%matplotlib osx
date = "2020-01-04 01:51" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("2020 AV2",date=date)


fig, ax  = rebound.OrbitPlot(sim,color=True);
fig = rebound.OrbitPlot(sim,slices=0.3,color=True,unitlabel="[AU]",xlim=[-1.1,1.1],ylim=[-1.1,1.1],fancy=True)
plt.text(.05, 1.2, date, horizontalalignment='center',verticalalignment='center')
plt.savefig('Solar_system_2020_01_04_with_2020_AV2.pdf')

fig, ax  = rebound.OrbitPlot(sim,color=True,fancy=True,lw=2,slice=0.3);

fig = rebound.OrbitPlot(sim,slices=0.3,color=True,unitlabel="[AU]",xlim=[-1.1,1.1],ylim=[-1.1,1.1],fancy=True)
plt.savefig('Solar_system_2020_01_04_with_2020_AV2_fancy.pdf',lw=3)


'''
#solid plot

import rebound
import datetime
year = 2.*np.pi # One year in units where G=1
#starting date of sim
date = "2020-01-04 01:51" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
#sim.save("ss.bin")#saves sim
#sim = rebound.Simulation("ss.bin")#load sim
sim.move_to_com()

%matplotlib osx
time=0.0
encounterdate = datetime.datetime.today().replace(month=1, day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
print encounterdate.strftime("%Y-%m-%d %H:%M")
fig = rebound.OrbitPlot(sim,color=True,unitlabel="(au)",xlim=[-1.1,1.1],ylim=[-1.1,1.1],figsize=(8,8),orbit_type="solid",lw=2,slices=0.3,periastron=False)
#plt.text(.1, .73, encounterdate.strftime("%Y-%m-%d %H:%M"), horizontalalignment='center',verticalalignment='center')
plt.savefig('orbit_plot_discovery.png')


#plot for MSIP proposal including
#solid plot 2019 AQ3 and 2019 LF6

import rebound
import datetime
year = 2.*np.pi # One year in units where G=1
#starting date of sim
date = "2020-01-04 01:51" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.add("Sun",date=date)
#sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("2019 AQ3",date=date)#can use CAP to get the most recent version
sim.add("2019 LF6",date=date)#can use CAP to get the most recent version
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
sim.add("2020 OV1",date=date)#can use CAP to get the most recent version
sim.add("2021 BS1",date=date)#can use CAP to get the most recent version
#sim.save("ss.bin")#saves sim
#sim = rebound.Simulation("ss.bin")#load sim
sim.move_to_com()

%matplotlib osx
time=0.0
encounterdate = datetime.datetime.today().replace(month=1, day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
print encounterdate.strftime("%Y-%m-%d %H:%M")
ax = rebound.OrbitPlot(sim,color=True,unitlabel="(au)",xlim=[-1.1,1.1],ylim=[-1.1,1.1],figsize=(8,8),orbit_type="solid",lw=2,slices=0.45,periastron=False)
#plt.text(-.92, .95, encounterdate.strftime("%Y-%m-%d %H:%M \n (UTC)"), horizontalalignment='center',verticalalignment='center')
plt.savefig('orbit_plot_atira_vatira_earth_venus_mjd_58852.png')

#for rotation 90 degs
%matplotlib osx
time=0.0
encounterdate = datetime.datetime.today().replace(month=1, day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
print encounterdate.strftime("%Y-%m-%d %H:%M")
fig,ax,ax1,ax2 = rebound.OrbitPlot(sim,color=True,unitlabel="(au)",xlim=[-1.1,1.1],ylim=[-1.1,1.1],figsize=(8,8),orbit_type="solid",lw=2,slices=0.45,periastron=False)
#plt.text(-.92, .95, encounterdate.strftime("%Y-%m-%d %H:%M \n (UTC)"), horizontalalignment='center',verticalalignment='center')
plt.setp(ax.get_xticklabels(), rotation=90)
plt.setp(ax.get_yticklabels(), rotation=90)
ax.set_xlabel('x (au)',rotation=90)
plt.setp(ax1.get_yticklabels(), rotation=90)
ax.set_xticks(np.arange(-1,1.25,0.25))
plt.savefig('orbit_plot_atira_vatira_earth_venus_mjd_58852_rot_90.png')


import rebound
import datetime
year = 2.*np.pi # One year in units where G=1
#starting date of sim
date = "2020-01-04 01:51" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.add("Sun",date=date)
#sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
#sim.save("ss.bin")#saves sim
#sim = rebound.Simulation("ss.bin")#load sim
sim.move_to_com()

%matplotlib osx
time=0.0
encounterdate = datetime.datetime.today().replace(month=1, day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
print encounterdate.strftime("%Y-%m-%d %H:%M")
ax = rebound.OrbitPlot(sim,color=True,unitlabel="(au)",xlim=[-1.1,1.1],ylim=[-1.1,1.1],figsize=(8,8),orbit_type="solid",lw=2,slices=0.45,periastron=False)
#plt.text(-.92, .95, encounterdate.strftime("%Y-%m-%d %H:%M \n (UTC)"), horizontalalignment='center',verticalalignment='center')
plt.savefig('orbit_plot_atira_vatira_earth_venus_mjd_58852_AV2_only.png')


#for rotation 90 degs
%matplotlib osx
time=0.0
encounterdate = datetime.datetime.today().replace(month=1, day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
print encounterdate.strftime("%Y-%m-%d %H:%M")
fig,ax,ax1,ax2 = rebound.OrbitPlot(sim,color=True,unitlabel="(au)",xlim=[-1.1,1.1],ylim=[-1.1,1.1],figsize=(8,8),orbit_type="solid",lw=2,slices=0.45,periastron=False)
#plt.text(-.92, .95, encounterdate.strftime("%Y-%m-%d %H:%M \n (UTC)"), horizontalalignment='center',verticalalignment='center')
plt.setp(ax.get_xticklabels(), rotation=90)
plt.setp(ax.get_yticklabels(), rotation=90)
ax.set_xlabel('x (au)',rotation=90)
plt.setp(ax1.get_yticklabels(), rotation=90)
ax.set_xticks(np.arange(-1,1.25,0.25))
plt.savefig('orbit_plot_atira_vatira_earth_venus_mjd_58852_AV2_only_rot90.png')



#animation plot
import rebound
import datetime
year = 2.*np.pi # One year in units where G=1
#starting date of sim
date = "2020-01-04 01:51" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = 0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
sim.save("ss.bin")#saves sim
sim = rebound.Simulation("ss.bin")#load sim

%matplotlib osx

from IPython.display import display, clear_output
import matplotlib.pyplot as plt
year = 2.*np.pi # One year in units where G=1
num_years = 1.
outputs_per_year = 365.256
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
sim.move_to_com()        # We always move to the center of momentum frame before an integration

for i,time in enumerate(times):#79 is 1 year
    sim.integrate(time)
    encounterdate = datetime.datetime.today().replace(day = 4, hour=1, minute=51, second=0) + datetime.timedelta(days=(time/year)*365.26)
    print encounterdate.strftime("%Y-%m-%d %H:%M")
    fig, ax = rebound.OrbitPlot(sim,color=True,unitlabel="[AU]",xlim=[-1.1,1.1],ylim=[-1.1,1.1])
    plt.text(.8, .95, encounterdate.strftime("%Y-%m-%d %H:%M"), horizontalalignment='center',verticalalignment='center', transform=ax.transAxes)
    #fig = rebound.OrbitPlot(sim,slices=0.7,color=True,unitlabel="[AU]",xlim=[-1.2,1.2],ylim=[-1.2,1.2])
    #plt.text(.05, 1.4, date, horizontalalignment='center',verticalalignment='center')
    plt.savefig('movies/'+str(i+1).zfill(6)+'.png')
    plt.close()


#backwards integrate 200 kyrs
import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_200K_backwards'
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = -0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -200000.
outputs_per_year = 100
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump('2020AV2_sims/first_run_200K/x_backwards.pyc')
np.copy(y[:4,:]).dump('2020AV2_sims/first_run_200K/y_backwards.pyc')
np.copy(z[:4,:]).dump('2020AV2_sims/first_run_200K/z_backwards.pyc')
a_au.dump('2020AV2_sims/first_run_200K/a_au_backwards.pyc')
e.dump('2020AV2_sims/first_run_200K/e_backwards.pyc')
i_rad.dump('2020AV2_sims/first_run_200K/i_rad_backwards.pyc')
omega_rad.dump('2020AV2_sims/first_run_200K/omega_backwards.pyc')
P_jacobian.dump('2020AV2_sims/first_run_200K/P_jacob_backwards.pyc')

#forward integrate 200 kyrs
import rebound
import datetime
sim = rebound.Simulation()
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_200K_forwards'
sim.dt = 0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 hdate = "2020-01-14 00:00" #syncrhonizes all particles to this date
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 200000.
outputs_per_year = 100
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros((4,Noutputs))
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start


np.copy(x[:4,:]).dump('2020AV2_sims/first_run_200K/x_forwards.pyc')
np.copy(y[:4,:]).dump('2020AV2_sims/first_run_200K/y_forwards.pyc')
np.copy(z[:4,:]).dump('2020AV2_sims/first_run_200K/z_forwards.pyc')
a_au.dump('2020AV2_sims/first_run_200K/a_au_forwards.pyc')
e.dump('2020AV2_sims/first_run_200K/e_forwards.pyc')
i_rad.dump('2020AV2_sims/first_run_200K/i_rad_forwards.pyc')
omega_rad.dump('2020AV2_sims/first_run_200K/omega_forwards.pyc')
P_jacobian.dump('2020AV2_sims/first_run_200K/P_jacob_forwards.pyc')


#backwards integrate 50 kyrs
import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_200K_backwards_test'
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = -0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -50000.
outputs_per_year = 10
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)
#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs
time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

num_years = -100000.
outputs_per_year = 10
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)
times_year = np.array([times_backward/year])[0]

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("a [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], a_au[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())


fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("e [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())


fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())

#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)

q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("perihelion,aphelion distance [au]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=1);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=1);
plt.axhline(y=0.728213,linewidth=4, color='red',linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color='red',linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color='green',linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color='green',linestyle='-.')
plt.xlim(times_year.min(),times_year.max())



#forwars integrate 50 kyrs
import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_200K_forwards_test'
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = 0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 50000.
outputs_per_year = 10
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)
#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs
time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start


num_years = 100000.
outputs_per_year = 10
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)
times_year = np.array([times_backward/year])[0]

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("a [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], a_au[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())


fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("e [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())


fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())

#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)


'''
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5,5))
ax = plt.subplot(111)
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
plt.plot(x[0], y[0]);
plt.plot(x[1], y[1]);
plt.plot(x[2], y[2]);
plt.plot(x[3], y[3]);
'''

#combine files and create plots for 200 kyr run
import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -200000.
outputs_per_year = 100
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

num_years = 200000.
outputs_per_year = 100
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)
times = np.concatenate([times_backward,times_forward])
times_year = np.array([times/year])[0]

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K/'
plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K/sims_plots/'

P_jacob_backwards = np.load(save_directory+'P_jacob_backwards.pyc')
P_jacob_forwards = np.load(save_directory+'P_jacob_forwards.pyc')
P_jacob = np.concatenate([P_jacob_backwards,P_jacob_forwards])

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards,a_au_forwards])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e = np.concatenate([e_backwards,e_forwards])

i_rad_backwards = np.load(save_directory+'i_rad_backwards.pyc')
i_rad_forwards = np.load(save_directory+'i_rad_forwards.pyc')
i_rad  = np.concatenate([i_rad_backwards,i_rad_forwards])

omega_backwards = np.load(save_directory+'omega_backwards.pyc')
omega_forwards = np.load(save_directory+'omega_forwards.pyc')
omega_rad  = np.concatenate([omega_backwards,omega_forwards])

x_backwards = np.load(save_directory+'x_backwards.pyc')
x_forwards = np.load(save_directory+'x_forwards.pyc')
x = np.concatenate([x_backwards,x_forwards],axis=1)

y_backwards = np.load(save_directory+'y_backwards.pyc')
y_forwards = np.load(save_directory+'y_forwards.pyc')
y = np.concatenate([y_backwards,y_forwards],axis=1)

z_backwards = np.load(save_directory+'z_backwards.pyc')
z_forwards = np.load(save_directory+'z_forwards.pyc')
z = np.concatenate([z_backwards,z_forwards],axis=1)


#moid mercury

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 100
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out])
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
#plt.xlim(times_year.min(),times_year.max())
#plt.savefig(plot_save_directory+'dist_mercury.png')

'''
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], distances_1_3[::separate_out]);

closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))


import datetime
encounterdate = datetime.datetime.today() + datetime.timedelta(days=365.25*closeencountertime)
encounterdate.strftime("%Y-%m-%d %H:%M")
'''

#moid venus

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 100
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
#plt.xlim(times_year.min(),times_year.max())
#plt.savefig(plot_save_directory+'dist_venus.png')


'''
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], distances_2_3[::separate_out]);


import datetime
encounterdate = datetime.datetime.today() + datetime.timedelta(days=365.25*closeencountertime)
encounterdate.strftime("%Y-%m-%d %H:%M")

closeencountertime = times[np.argmin(distance)]/year
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
'''

#a

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]",fontsize=24)
ax.set_ylabel("a [AU]",fontsize=24)
separate_out = 100
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out]/1.0e6, a_au[::separate_out],'-',markersize=1);
plt.xlim(times_year.min()/1.0e6,times_year.max()/1.0e6)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.savefig('a_au_200_Kyr.png')


#e

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("e")
separate_out = 100
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.tick_params(labelsize=28)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.savefig(plot_save_directory+'e.png')


#i

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("i [deg]")
separate_out = 100
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.tick_params(labelsize=28)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.savefig(plot_save_directory+'i.png')

#Kozai parameter
kozai = ((1.-e**2)**.5) * np.cos(i_rad)
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel((r'$(1.-e^2)^{1/2} cos(i)$'))
separate_out = 100
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], kozai[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'kozai.png')


#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 100
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs*2 * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)
plt.savefig(plot_save_directory+'omega.png')


#peri,aphelion

q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [yrs]")
ax.set_ylabel("perihelion,aphelion distance [au]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=1);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=1);
plt.axhline(y=0.728213,linewidth=4, color='red',linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color='red',linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color='green',linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color='green',linestyle='-.')
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'qQ.png')

#200 k coarser

#use for science paper revisions

#backwards

import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K_coarse/'
sim_name = backup_dir + '2020AV2_200K_coarse_backwards'
date = "2020-11-26 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = -0.03 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -200000.
outputs_per_year = 30
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    #print i
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
        a_au.dump(dump_dir + 'a_au_forwards.pyc')
        e.dump(dump_dir + 'e_forwards.pyc')
        i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
        omega_rad.dump(dump_dir + 'omega_forwards.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump(dump_dir + 'x_backwards.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_backwards.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_backwards.pyc')
a_au.dump(dump_dir + 'a_au_backwards.pyc')
e.dump(dump_dir + 'e_backwards.pyc')
i_rad.dump(dump_dir + 'i_rad_backwards.pyc')
omega_rad.dump(dump_dir + 'omega_backwards.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_backwards.pyc')

#forwards

import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K_coarse/'
sim_name = backup_dir + '2020AV2_200K_coarse_forwards'
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = 0.03 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 200000.
outputs_per_year = 30
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
        a_au.dump(dump_dir + 'a_au_forwards.pyc')
        e.dump(dump_dir + 'e_forwards.pyc')
        i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
        omega_rad.dump(dump_dir + 'omega_forwards.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
a_au.dump(dump_dir + 'a_au_forwards.pyc')
e.dump(dump_dir + 'e_forwards.pyc')
i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
omega_rad.dump(dump_dir + 'omega_forwards.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')

#plot coarser

import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -200000.
outputs_per_year = 30
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

num_years = 200000.
outputs_per_year = 30
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)
times = np.concatenate([times_backward,times_forward])
times_year = np.array([times/year])[0]




save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K_coarse/'
plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_200K_coarse/sims_plots/'

P_jacob_backwards = np.load(save_directory+'P_jacob_backwards.pyc')
P_jacob_forwards = np.load(save_directory+'P_jacob_forwards.pyc')
P_jacob = np.concatenate([P_jacob_backwards,P_jacob_forwards])

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards,a_au_forwards])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e = np.concatenate([e_backwards,e_forwards])

i_rad_backwards = np.load(save_directory+'i_rad_backwards.pyc')
i_rad_forwards = np.load(save_directory+'i_rad_forwards.pyc')
i_rad  = np.concatenate([i_rad_backwards,i_rad_forwards])

omega_backwards = np.load(save_directory+'omega_backwards.pyc')
omega_forwards = np.load(save_directory+'omega_forwards.pyc')
omega_rad  = np.concatenate([omega_backwards,omega_forwards])

x_backwards = np.load(save_directory+'x_backwards.pyc')
x_forwards = np.load(save_directory+'x_forwards.pyc')
x = np.concatenate([x_backwards,x_forwards],axis=1)

y_backwards = np.load(save_directory+'y_backwards.pyc')
y_forwards = np.load(save_directory+'y_forwards.pyc')
y = np.concatenate([y_backwards,y_forwards],axis=1)

z_backwards = np.load(save_directory+'z_backwards.pyc')
z_forwards = np.load(save_directory+'z_forwards.pyc')
z = np.concatenate([z_backwards,z_forwards],axis=1)


#moid mercury

fig = plt.figure(figsize=(18,7.5))
#ax = plt.subplot(111)
#ax.set_xlabel("time [Myrs]")
#ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 300
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out])
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
#plt.xlim(times_year.min(),times_year.max())
plt.savefig('moid_mercury_200_Kyr.png')


#moid venus

fig = plt.figure(figsize=(18,7.5))
#ax = plt.subplot(111)
#ax.set_xlabel("time [Myrs]")
#ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 300
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out])
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
#plt.xlim(times_year.min(),times_year.max())
plt.savefig('moid_venus_200_Kyr.png')


#a


fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("Time (Myrs)",fontsize=24)
ax.set_ylabel("a (au)",fontsize=24)
separate_out = 30
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out]/1.0e6, a_au[::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min()/1.0e6,times_year.max()/1.0e6)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=24)
plt.yticks(fontsize=24)
plt.savefig('a_au_200_Kyr.png')



#e

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("e")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[:Noutputs][::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())


#i

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())

#Kozai parameter
kozai = ((1.-e**2)**.5) * np.cos(i_rad)
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel((r'$(1.-e^2)^{1/2} cos(i)$'))
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], kozai[::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())


#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs*2 * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)

#peri,aphelion

q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("perihelion,aphelion distance [au]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=0.3);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=0.3);
plt.axhline(y=0.728213,linewidth=4, color='red',linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color='red',linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color='green',linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color='green',linestyle='-.')
plt.axhline(y=1.017,linewidth=4, color='purple',linestyle=':')
plt.axhline(y=0.98327,linewidth=4, color='purple',linestyle='-.')
plt.xlim(times_year.min(),times_year.max())



#1 Myrs time scale run

#backwards

import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_1Mil_backwards'
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = -0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 h
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -1000000.
outputs_per_year = 5
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump('2020AV2_sims/second_run_1mil/x_backwards.pyc')
np.copy(y[:4,:]).dump('2020AV2_sims/second_run_1mil/y_backwards.pyc')
np.copy(z[:4,:]).dump('2020AV2_sims/second_run_1mil/z_backwards.pyc')
a_au.dump('2020AV2_sims/second_run_1mil/a_au_backwards.pyc')
e.dump('2020AV2_sims/second_run_1mil/e_backwards.pyc')
i_rad.dump('2020AV2_sims/second_run_1mil/i_rad_backwards.pyc')
omega_rad.dump('2020AV2_sims/second_run_1mil/omega_backwards.pyc')
P_jacobian.dump('2020AV2_sims/second_run_1mil/P_jacob_backwards.pyc')

#forward integrate 1Myrs
import rebound
import datetime
sim = rebound.Simulation()
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
sim_name = backup_dir + '2020AV2_1Mil_forwards'
sim.dt = 0.01 #(0.01/(np.pi * 2)) = 0.0015915494309189536 of the Earth's orbit, i.e. 365.256 days * (0.01/(np.pi * 2)) = 0.5813229789397333 days or ~14 hdate = "2020-01-14 00:00" #syncrhonizes all particles to this date
date = "2020-01-14 00:00" #syncrhonizes all particles to this date
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 1000000.
outputs_per_year = 5
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros((4,Noutputs))
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start


np.copy(x[:4,:]).dump('2020AV2_sims/second_run_1mil/x_forwards.pyc')
np.copy(y[:4,:]).dump('2020AV2_sims/second_run_1mil/y_forwards.pyc')
np.copy(z[:4,:]).dump('2020AV2_sims/second_run_1mil/z_forwards.pyc')
a_au.dump('2020AV2_sims/second_run_1mil/a_au_forwards.pyc')
e.dump('2020AV2_sims/second_run_1mil/e_forwards.pyc')
i_rad.dump('2020AV2_sims/second_run_1mil/i_rad_forwards.pyc')
omega_rad.dump('2020AV2_sims/second_run_1mil/omega_forwards.pyc')
P_jacobian.dump('2020AV2_sims/second_run_1mil/P_jacob_forwards.pyc')

#plots for 1 mil run
import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -1000000.
outputs_per_year = 5
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

num_years = 1000000.
outputs_per_year = 5
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)
times = np.concatenate([times_backward,times_forward])
times_year = np.array([times/year])[0]/1e6

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/second_run_1mil/'
plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/second_run_1mil/sims_plots/'

P_jacob_backwards = np.load(save_directory+'P_jacob_backwards.pyc')
P_jacob_forwards = np.load(save_directory+'P_jacob_forwards.pyc')
P_jacob = np.concatenate([P_jacob_backwards,P_jacob_forwards])

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards,a_au_forwards])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e = np.concatenate([e_backwards,e_forwards])

i_rad_backwards = np.load(save_directory+'i_rad_backwards.pyc')
i_rad_forwards = np.load(save_directory+'i_rad_forwards.pyc')
i_rad  = np.concatenate([i_rad_backwards,i_rad_forwards])

omega_backwards = np.load(save_directory+'omega_backwards.pyc')
omega_forwards = np.load(save_directory+'omega_forwards.pyc')
omega_rad  = np.concatenate([omega_backwards,omega_forwards])

x_backwards = np.load(save_directory+'x_backwards.pyc')
x_forwards = np.load(save_directory+'x_forwards.pyc')
x = np.concatenate([x_backwards,x_forwards],axis=1)

y_backwards = np.load(save_directory+'y_backwards.pyc')
y_forwards = np.load(save_directory+'y_forwards.pyc')
y = np.concatenate([y_backwards,y_forwards],axis=1)

z_backwards = np.load(save_directory+'z_backwards.pyc')
z_forwards = np.load(save_directory+'z_forwards.pyc')
z = np.concatenate([z_backwards,z_forwards],axis=1)


#moid mercury

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'dist_mercury_2mil.png')

#moid venus

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'dist_venus_2mil.png')


#a

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("a [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], a_au[::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'a_au_2mil.png')


#e

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("e")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'e_2mil.png')


#i

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'i_2mil.png')

#Kozai parameter
kozai = ((1.-e**2)**.5) * np.cos(i_rad)
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel((r'$(1.-e^2)^{1/2} cos(i)$'))
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], kozai[::separate_out],'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'kozai_2mil.png')


#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs*2 * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=0.3);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)
plt.savefig(plot_save_directory+'omega_2mil.png')

#peri,aphelion

q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("perihelion,aphelion distance [au]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=0.3);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=0.3);
plt.axhline(y=0.728213,linewidth=4, color='red',linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color='red',linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color='green',linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color='green',linestyle='-.')
plt.axhline(y=1.017,linewidth=4, color='purple',linestyle=':')
plt.axhline(y=0.98327,linewidth=4, color='purple',linestyle='-.')
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'qQ_2mil.png')


#10 Myrs time scale run

#backwards

import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
sim_name = backup_dir + '2020AV2_10mil_backwards'
date = "2020-11-26 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = -0.03 #(0.1/(np.pi * 2)) = 0.00477464829275686 of the Earth's orbit, i.e. 365.256 * (0.03/(np.pi * 2)) = 1.7439689368191995 days or < 88 days/50, 1/50th of mercury's orbit
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    #print i
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
        a_au.dump(dump_dir + 'a_au_forwards.pyc')
        e.dump(dump_dir + 'e_forwards.pyc')
        i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
        omega_rad.dump(dump_dir + 'omega_forwards.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump(dump_dir + 'x_backwards.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_backwards.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_backwards.pyc')
a_au.dump(dump_dir + 'a_au_backwards.pyc')
e.dump(dump_dir + 'e_backwards.pyc')
i_rad.dump(dump_dir + 'i_rad_backwards.pyc')
omega_rad.dump(dump_dir + 'omega_backwards.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_backwards.pyc')

#forwards

import rebound
import datetime
#sim_name
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
sim_name = backup_dir + '2020AV2_10mil_forwards'
date = "2020-11-26 00:00" #syncrhonizes all particles to this date
sim = rebound.Simulation()
sim.dt = 0.03 #(0.1/(np.pi * 2)) = 0.00477464829275686 of the Earth's orbit, i.e. 365.256 * (0.03/(np.pi * 2)) = 1.7439689368191995 days or < 88 days/50, 1/50th of mercury's orbit
sim.add("Sun",date=date)
sim.add("Mercury",date=date) #syncrhonizes all particles to this date
sim.add("Venus",date=date)
sim.add("Earth",date=date)
sim.add("Mars",date=date)
sim.add("Jupiter",date=date)
sim.add("Saturn",date=date)
sim.add("2020 AV2",date=date)#can use CAP to get the most recent version
sim.status()

#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)

#does the actual integration
sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
sim.move_to_com()        # We always move to the center of momentum frame before an integration
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
        a_au.dump(dump_dir + 'a_au_forwards.pyc')
        e.dump(dump_dir + 'e_forwards.pyc')
        i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
        omega_rad.dump(dump_dir + 'omega_forwards.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start

np.copy(x[:4,:]).dump(dump_dir + 'x_forwards.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_forwards.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_forwards.pyc')
a_au.dump(dump_dir + 'a_au_forwards.pyc')
e.dump(dump_dir + 'e_forwards.pyc')
i_rad.dump(dump_dir + 'i_rad_forwards.pyc')
omega_rad.dump(dump_dir + 'omega_forwards.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_forwards.pyc')


#plots for 10 mil year run

import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)
times = np.concatenate([times_backward,times_forward])
times_year = np.array([times/year])[0]/1e6

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/sims_plots/'

P_jacob_backwards = np.load(save_directory+'P_jacob_backwards.pyc')
P_jacob_forwards = np.load(save_directory+'P_jacob_forwards.pyc')
P_jacob = np.concatenate([P_jacob_backwards,P_jacob_forwards])

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards,a_au_forwards])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e = np.concatenate([e_backwards,e_forwards])

i_rad_backwards = np.load(save_directory+'i_rad_backwards.pyc')
i_rad_forwards = np.load(save_directory+'i_rad_forwards.pyc')
i_rad  = np.concatenate([i_rad_backwards,i_rad_forwards])

omega_backwards = np.load(save_directory+'omega_backwards.pyc')
omega_forwards = np.load(save_directory+'omega_forwards.pyc')
omega_rad  = np.concatenate([omega_backwards,omega_forwards])

x_backwards = np.load(save_directory+'x_backwards.pyc')
x_forwards = np.load(save_directory+'x_forwards.pyc')
x = np.concatenate([x_backwards,x_forwards],axis=1)

y_backwards = np.load(save_directory+'y_backwards.pyc')
y_forwards = np.load(save_directory+'y_forwards.pyc')
y = np.concatenate([y_backwards,y_forwards],axis=1)

z_backwards = np.load(save_directory+'z_backwards.pyc')
z_forwards = np.load(save_directory+'z_forwards.pyc')
z = np.concatenate([z_backwards,z_forwards],axis=1)


#moid mercury

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 10
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig('dist_mercury_10mil.png')

#moid venus

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig('dist_venus_10mil.png')


#a

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("a [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], a_au[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig('a_au_10mil.png')


#e

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("e")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig('e_10mil.png')


#i

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig('i_10mil.png')

#Kozai parameter
kozai = ((1.-e**2)**.5) * np.cos(i_rad)
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel((r'$(1.-e^2)^{1/2} cos(i)$'))
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], kozai[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig('kozai_10mil.png')


#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(Noutputs*2 * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)
plt.savefig('omega_10mil.png')

#peri,aphelion

q_au, Q_au = a_au*(1-e),a_au*(1+e)
colors = [(1.,0.,0.),(0.,0.75,0.75),(0.75,0.,0.75),(0.75, 0.75, 0,),(0., 0., 0.),(0., 0., 1.),(0., 0.5, 0.)]

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("Time (Myrs)",fontsize=24)
ax.set_ylabel("Perihelion, aphelion distance (au)",fontsize=24)
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=1);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=1);
plt.axhline(y=0.728213,linewidth=4, color=colors[1],linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color=colors[1],linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color=colors[0],linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color=colors[0],linestyle='-.')
plt.axhline(y=1.017,linewidth=4, color=colors[2],linestyle=':')
plt.axhline(y=0.98327,linewidth=4, color=colors[2],linestyle='-.')
plt.xlim(times_year.min(),times_year.max())
plt.tick_params(labelsize=25)
plt.xticks(fontsize=25)
plt.yticks(fontsize=25)
plt.savefig('qQ_10mil.png')


#extend 10 Myrs time scale to 30 Myrs continuing where we left off from 10 Myrs run
#backwards
import rebound
import datetime
#sim_name
direction = 'backwards'
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/'
sim = rebound.Simulation(backup_dir + '2020AV2_10mil_backwards')
sim_name = backup_dir + '2020AV2_30mil_backwards'
#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = -20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs) + times[-1]
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)
#does the actual integration

sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    #print i
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_'+direction+'.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_'+direction+'.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_'+direction+'.pyc')
        a_au.dump(dump_dir + 'a_au_'+direction+'.pyc')
        e.dump(dump_dir + 'e_'+direction+'.pyc')
        i_rad.dump(dump_dir + 'i_rad_'+direction+'.pyc')
        omega_rad.dump(dump_dir + 'omega_'+direction+'.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_'+direction+'.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start
np.copy(x[:4,:]).dump(dump_dir + 'x_'+direction+'.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_'+direction+'.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_'+direction+'.pyc')
a_au.dump(dump_dir + 'a_au_'+direction+'.pyc')
e.dump(dump_dir + 'e_'+direction+'.pyc')
i_rad.dump(dump_dir + 'i_rad_'+direction+'.pyc')
omega_rad.dump(dump_dir + 'omega_'+direction+'.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_'+direction+'.pyc')

#forwards
#sim_name
#extend 10 Myrs time scale to 30 Myrs continuing where we left off from 10 Myrs run
#backwards
import rebound
import datetime
#sim_name
direction = 'forwards'
backup_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/sim_backups/'
dump_dir = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/'
sim = rebound.Simulation(backup_dir + '2020AV2_10mil_forwards')
sim_name = backup_dir + '2020AV2_30mil_forwards'
#sets up storgage arrays
import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = 20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs) + times[-1]
x = np.zeros((4,Noutputs))
y = np.zeros((4,Noutputs))
z = np.zeros((4,Noutputs))
#orbit
a_au = np.zeros(Noutputs)
e = np.zeros(Noutputs)
i_rad = np.zeros(Noutputs)
Omega_rad = np.zeros(Noutputs)
omega_rad = np.zeros(Noutputs)
pomega_rad = np.zeros(Noutputs)
l_rad = np.zeros(Noutputs)
P_jacobian = np.zeros(Noutputs)
n_deg_p_day = np.zeros(Noutputs)
distances_1_3 = np.zeros(Noutputs)
distances_2_3 = np.zeros(Noutputs)
#does the actual integration

sim.integrator = "ias15" # IAS15 is the default integrator, so we actually don't need this line
ps = sim.particles       # ps is now an array of pointers and will change as the simulation runs

time_start = datetime.datetime.now()
for i,time in enumerate(times):
    #print i
    if i % (Noutputs/100.)==0:
        print 'Step ' + str(i) + ' out of ' + str(Noutputs) + ', saving.'
        sim.save(sim_name)#saves sim
    if i % (Noutputs/10.)==0: #dumps 10 times during sims
        print 'Dumping on '+ str(i) + ' out of ' + str(Noutputs)
        np.copy(x[:4,:]).dump(dump_dir + 'x_'+direction+'.pyc')
        np.copy(y[:4,:]).dump(dump_dir + 'y_'+direction+'.pyc')
        np.copy(z[:4,:]).dump(dump_dir + 'z_'+direction+'.pyc')
        a_au.dump(dump_dir + 'a_au_'+direction+'.pyc')
        e.dump(dump_dir + 'e_'+direction+'.pyc')
        i_rad.dump(dump_dir + 'i_rad_'+direction+'.pyc')
        omega_rad.dump(dump_dir + 'omega_'+direction+'.pyc')
        P_jacobian.dump(dump_dir + 'P_jacob_'+direction+'.pyc')
        print 'Dumping complete.'
    sim.integrate(time)
    x[0][i] = ps[1].x   # This stores the data which allows us to plot it later
    y[0][i] = ps[1].y #index 2 is the third partcile Venus, index 4 is the fifth particle 2020 AV2
    z[0][i] = ps[1].z #for ps i = 1 mercury, i = 2 venus, i = 3 earth, i = 4 mars, i = 5 jupiter, i = 6 Saturn, i = 7 2020 AV2
    x[1][i] = ps[2].x
    y[1][i] = ps[2].y
    z[1][i] = ps[2].z
    x[2][i] = ps[3].x
    y[2][i] = ps[3].y
    z[2][i] = ps[3].z
    x[3][i] = ps[7].x
    y[3][i] = ps[7].y
    z[3][i] = ps[7].z
    #dp_1_3 = ps[1] - ps[3]   # Calculates the coponentwise difference between particles
    #dp_2_3 = ps[2] - ps[3]   # Calculates the coponentwise difference between particles
    #distances_1_3[i] = np.sqrt(dp_1_3.x*dp_1_3.x+dp_1_3.y*dp_1_3.y+dp_1_3.z*dp_1_3.z)
    #distances_2_3[i] = np.sqrt(dp_2_3.x*dp_2_3.x+dp_2_3.y*dp_2_3.y+dp_2_3.z*dp_2_3.z)
    orbit = ps[7].calculate_orbit(primary=ps[0])
    a_au[i] = orbit.a
    e[i] = orbit.e
    i_rad[i] = orbit.inc
    #Omega_rad[i] = orbit.Omega
    omega_rad[i] = orbit.omega
    #pomega_rad[i] = orbit.pomega
    #l_rad[i] = orbit.l
    P_jacobian[i] = orbit.P
    #n_deg_p_day[i] = orbit.n
time_end = datetime.datetime.now()
print time_end - time_start
np.copy(x[:4,:]).dump(dump_dir + 'x_'+direction+'.pyc')
np.copy(y[:4,:]).dump(dump_dir + 'y_'+direction+'.pyc')
np.copy(z[:4,:]).dump(dump_dir + 'z_'+direction+'.pyc')
a_au.dump(dump_dir + 'a_au_'+direction+'.pyc')
e.dump(dump_dir + 'e_'+direction+'.pyc')
i_rad.dump(dump_dir + 'i_rad_'+direction+'.pyc')
omega_rad.dump(dump_dir + 'omega_'+direction+'.pyc')
P_jacobian.dump(dump_dir + 'P_jacob_'+direction+'.pyc')

#60 mill sim plots

import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = -20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward_2 = np.linspace(0.,num_years*year, Noutputs) + times[-1]

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = 20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward_2 = np.linspace(0.,num_years*year, Noutputs) + times[-1]


times = np.concatenate([times_backward_2,times_backward,times_forward,times_forward_2])
times_year = np.array([times/year])[0]/1e6

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
save_directory2 = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/'

plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/sims_plots/'

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au_backwards2 = np.load(save_directory2+'a_au_backwards.pyc')
a_au_forwards2 = np.load(save_directory2+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards2,a_au_backwards,a_au_forwards,a_au_forwards2])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e_backwards2 = np.load(save_directory2+'e_backwards.pyc')
e_forwards2 = np.load(save_directory2+'e_forwards.pyc')
e = np.concatenate([e_backwards2,e_backwards,e_forwards,e_forwards2])

i_rad_backwards = np.load(save_directory+'i_rad_backwards.pyc')
i_rad_forwards = np.load(save_directory+'i_rad_forwards.pyc')
i_rad_backwards2 = np.load(save_directory2+'i_rad_backwards.pyc')
i_rad_forwards2 = np.load(save_directory2+'i_rad_forwards.pyc')
i_rad = np.concatenate([i_rad_backwards2,i_rad_backwards,i_rad_forwards,i_rad_forwards2])

omega_backwards = np.load(save_directory+'omega_backwards.pyc')
omega_forwards = np.load(save_directory+'omega_forwards.pyc')
omega_backwards2 = np.load(save_directory2+'omega_backwards.pyc')
omega_forwards2 = np.load(save_directory2+'omega_forwards.pyc')
omega_rad = np.concatenate([omega_backwards2,omega_backwards,omega_forwards,omega_forwards2])

x_backwards = np.load(save_directory+'x_backwards.pyc')
x_forwards = np.load(save_directory+'x_forwards.pyc')
x_backwards2 = np.load(save_directory2+'x_backwards.pyc')
x_forwards2 = np.load(save_directory2+'x_forwards.pyc')
x = np.concatenate([x_backwards2,x_backwards,x_forwards,x_forwards2],axis =1)

y_backwards = np.load(save_directory+'y_backwards.pyc')
y_forwards = np.load(save_directory+'y_forwards.pyc')
y_backwards2 = np.load(save_directory2+'y_backwards.pyc')
y_forwards2 = np.load(save_directory2+'y_forwards.pyc')
y = np.concatenate([y_backwards2,y_backwards,y_forwards,y_forwards2],axis =1)

z_backwards = np.load(save_directory+'x_backwards.pyc')
z_forwards = np.load(save_directory+'x_forwards.pyc')
z_backwards2 = np.load(save_directory2+'x_backwards.pyc')
z_forwards2 = np.load(save_directory2+'x_forwards.pyc')
z = np.concatenate([z_backwards2,z_backwards,z_forwards,z_forwards2],axis =1)


#moid mercury

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 0,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'dist_mercury_30mil.png')

#moid venus

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 1,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'dist_venus_30mil.png')

#moid Earth

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("distance [AU]")
#compare
compare_i, compare_j = 2,3
separate_out = 1
distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j])+np.square(z[compare_i]-z[compare_j]))
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.semilogy(times_year[::separate_out], distance[::separate_out]);
#plt.ylim(1e-2, 2.0)
closeencountertime = times[np.argmin(distance)]/year
print("Minimum distance (%f AU) occured at time: %f years." % (np.min(distance),closeencountertime))
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'dist_earth_30mil.png')

#a

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("a [AU]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], a_au[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,5)
plt.savefig(plot_save_directory+'a_au_30mil.png')


#e

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("e")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], e[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,2)
plt.savefig(plot_save_directory+'e_30mil.png')


#i

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("i [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], np.degrees(i_rad[::separate_out]),'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'i_30mil.png')

#Kozai parameter
kozai = ((1.-e**2)**.5) * np.cos(i_rad)
fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel((r'$(1.-e^2)^{1/2} cos(i)$'))
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], kozai[::separate_out],'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.savefig(plot_save_directory+'kozai_30mil.png')


#omega

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("time [Myrs]")
ax.set_ylabel("arg. peri [deg]")
separate_out = 1
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
omega_deg = [angles_in_radians_zeroTo360_out_degrees(omega_rad[::separate_out][i]) for i in range(int(len(omega_rad) * (1./separate_out)))]
plt.plot(times_year[::separate_out], omega_deg,'.',markersize=1);
plt.xlim(times_year.min(),times_year.max())
plt.ylim(0.0,360.)
plt.savefig(plot_save_directory+'omega_30mil.png')

#peri,aphelion
colors = [(1.,0.,0.),(0.,0.75,0.75),(0.75,0.,0.75),(0.75, 0.75, 0,),(0., 0., 0.),(0., 0., 1.),(0., 0.5, 0.)]
q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("Time (Myrs)",fontsize=24)
ax.set_ylabel("Perihelion, aphelion distance (au)",fontsize=24)
separate_out = 2
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=2);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=2);
plt.axhline(y=0.728213,linewidth=4, color=colors[1],linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color=colors[1],linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color=colors[0],linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color=colors[0],linestyle='-.')
plt.axhline(y=1.017,linewidth=4, color=colors[2],linestyle=':')
plt.axhline(y=0.98327,linewidth=4, color=colors[2],linestyle='-.')
plt.axhline(y=1.666,linewidth=4, color=colors[4],linestyle=':')
plt.axhline(y=1.382,linewidth=4, color=colors[4],linestyle='-.')
plt.axhline(y=5.4588,linewidth=4, color=colors[6],linestyle=':')
plt.axhline(y=4.9501,linewidth=4, color=colors[6],linestyle='-.')
plt.xlim(-10,times_year.max())
plt.ylim(0.01,3)
plt.tick_params(labelsize=25)
plt.xticks(fontsize=25)
plt.yticks(fontsize=25)
plt.savefig('qQ_30mil.png')



#peri,aphelion
colors = [(1.,0.,0.),(0.,0.75,0.75),(0.75,0.,0.75),(0.75, 0.75, 0,),(0., 0., 0.),(0., 0., 1.),(0., 0.5, 0.)]
q_au, Q_au = a_au*(1-e),a_au*(1+e)

fig = plt.figure(figsize=(18,7.5))
ax = plt.subplot(111)
ax.set_xlabel("Time (Myrs)",fontsize=24)
ax.set_ylabel("Perihelion, aphelion distance (au)",fontsize=24)
separate_out = 2
#distance = np.sqrt(np.square(x[compare_i]-x[compare_j])+np.square(y[compare_i]-y[compare_j]))
plt.plot(times_year[::separate_out], q_au[::separate_out],'.',markersize=2);
plt.plot(times_year[::separate_out], Q_au[::separate_out],'.',markersize=2);
plt.axhline(y=0.728213,linewidth=4, color=colors[1],linestyle=':')
plt.axhline(y=0.718440,linewidth=4, color=colors[1],linestyle='-.')
plt.axhline(y=0.466697,linewidth=4, color=colors[0],linestyle=':')
plt.axhline(y=0.307499,linewidth=4, color=colors[0],linestyle='-.')
plt.axhline(y=1.017,linewidth=4, color=colors[2],linestyle=':')
plt.axhline(y=0.98327,linewidth=4, color=colors[2],linestyle='-.')
plt.axhline(y=1.666,linewidth=4, color=colors[4],linestyle=':')
plt.axhline(y=1.382,linewidth=4, color=colors[4],linestyle='-.')
plt.axhline(y=5.4588,linewidth=4, color=colors[6],linestyle=':')
plt.axhline(y=4.9501,linewidth=4, color=colors[6],linestyle='-.')
plt.xlim(-10,28)
plt.ylim(0.01,3)
plt.tick_params(labelsize=25)
plt.xticks(fontsize=25)
plt.yticks(fontsize=25)
plt.savefig('qQ_28mil_v2.png')

#optical colors

#color plot
#BZ509 test
#Zeljko colors
from astroML.datasets import fetch_moving_objects
from astroML.plotting.tools import devectorize_axes
data = fetch_moving_objects(Parker2008_cuts=True)
mag_a = data['mag_a']
mag_g = data['mag_g']
mag_r = data['mag_r']
mag_i = data['mag_i']
mag_z = data['mag_z']
a = data['aprime']
sini = data['sin_iprime']

# dither: magnitudes are recorded only to +/- 0.01
mag_a += -0.005 + 0.01 * np.random.random(size=mag_a.shape)
mag_g += -0.005 + 0.01 * np.random.random(size=mag_g.shape)
mag_r += -0.005 + 0.01 * np.random.random(size=mag_r.shape)
mag_i += -0.005 + 0.01 * np.random.random(size=mag_i.shape)
mag_z += -0.005 + 0.01 * np.random.random(size=mag_z.shape)
mag_i_minuz_z = mag_i - mag_z
family_name = "all_mb_a_astar_vs_i_minus_z"
zcolors = zeljko_colors(mag_a,mag_i-mag_z)

font1=20
a_star_plot,i_minus_z_plot, colors, family_name = mag_a, mag_i-mag_z, zcolors, family_name
paperheight = 10*1.2
paperwidth = 7.28*1.2*1.45
margin = 1.0
fig = plt.figure(figsize=(paperwidth - 2*margin, paperheight - 2*margin),facecolor='k')
ax = black_bg_subplot(111)
ax.scatter(mag_g - mag_r, mag_r - mag_z, c=colors, s=10, lw=0)
ax.set_xlim(0.14, 1.05)
ax.set_ylim(-0.6, 1.2)
ax.set_xlabel('g-r', color='w',fontsize=font1)
ax.set_ylabel('r-z', color='w',fontsize=font1)
ya = ax.yaxis
xa = ax.xaxis
ya.set_tick_params(color='white',labelsize=font1)
xa.set_tick_params(color='white',labelsize=font1)
g_r_i_z_tnos = np.loadtxt('ofek_TNO_colors_g_r_i_z_only_dirty')
g = g_r_i_z_tnos[:,0]
g_unc = np.zeros(len(g))
r = g_r_i_z_tnos[:,1]
r_unc = np.zeros(len(r))
i = g_r_i_z_tnos[:,2]
i_unc = np.zeros(len(i))
z = g_r_i_z_tnos[:,3]
z_unc = np.zeros(len(z))
ax.scatter(0.44, 0.14, edgecolors = "black",c="yellow", marker = "o", linewidth=2,s=300,label=r'$\mathrm{Sun}$')
ax.scatter(g-r, r-z, edgecolors = "#00BDFF",c="#FF0036", marker = "*", linewidth=2,s=300,label=r'$\mathrm{TNOs, \, Ofek \, (2012)}$')
ax.scatter(0.482, 0.233, edgecolors = "#003EFF",c="#003EFF", marker = ".", linewidth=2,s=10,label=r'$\mathrm{C-types, \, Juri\acute{c} \, (2002)}$')
ax.scatter(0.6882, 0.3215, edgecolors = "#FFAE00",c="#FFAE00", marker = ".", linewidth=2,s=10,label=r'$\mathrm{S-types, \, Juri\acute{c} \, (2002)}$')
ax.scatter(0.6882, -0.1866, edgecolors = "#27FF00",c="#27FF00", marker = ".", linewidth=2,s=10,label=r'$\mathrm{V-types, \, Juri\acute{c} \, (2002)}$')
# comets
umg_c,umg_cpm,gmr_c,gmr_cpm,rmi_c,rmi_cpm,imz_c,imz_cpm = np.loadtxt('solontoi_2012_u_m_g_g_m_r_r_m_i_i_m_z').T
rmz_c = rmi_c + imz_c
rmz_cpm  = np.hypot(rmi_cpm,imz_cpm)
ax.scatter(gmr_c, rmz_c, edgecolors = "#007425",c="yellow", marker = "*", linewidth=2,s=300,label=r'$\mathrm{Comets, \, Solontoi \, (2012)}$')

#2020-01-08 colors
g2, gunc2 = 18.1,0.05
r2, runc2 = 17.528,0.03
i2, iunc2 = 17.409,0.05
z2, zunc2 = 17.35,0.04
gmr2, gmrunc2 = g2-r2,np.hypot(gunc2,runc2)
rmi2, rmiunc2 = r2-i2,np.hypot(runc2,iunc2)
imz2, imzunc2 = i2-z2,np.hypot(iunc2,zunc2)
rmz2 = rmi2 + imz2
rmzunc2 = np.hypot(gmrunc2,imzunc2)

line1 = ax.errorbar(gmr2,rmz2, xerr=gmrunc2, yerr=rmzunc2, fmt='o', ecolor = '#F80004',color='#FF9B00', capthick=150, lw=6,label=r'$\mathrm{2020\,AV2, \, P200, \, 2020-01-13}$')
legend = ax.legend(loc='lower left',prop={'size':12})
legend.get_frame().set_alpha(0.5)
frame = legend.get_frame()
frame.set_facecolor('black')
for text in legend.get_texts():
    text.set_color("white")
plt.tight_layout()
plt.savefig('2020AV2_SDSS_Solar_System_colors_grz.pdf',facecolor=fig.get_facecolor(), edgecolor='white')


#MLO 2020 01 14 and 2020 01 15


#do weighted averages of R
lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original = np.loadtxt('ztf09k5/MLO/20200114/AV2_MLO40_phot.dat',usecols=(0,5,6))
lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,0] -=2400000.5
lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original = np.loadtxt('ztf09k5/MLO/20200115/AV2_MLO40_phot.dat',usecols=(0,5,6))
lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,0] -=2400000.5

#de phase and de delta and de r
PA_degrees = 97.1
G = 0.15 #standard for C complex
ast_hel_dist = 0.597
g_dist = 0.711

PA_degrees_norm = 0
G = 0.15 #standard for C complex
ast_hel_dist_norm = 1
g_dist_norm = 1



lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,1] = lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,1] - phaseFunction(np.radians(PA_degrees),G, ast_hel_dist, g_dist , 'HG') + phaseFunction(np.radians(PA_degrees_norm),G, ast_hel_dist_norm, g_dist_norm , 'HG')

mag_correction = -0.25
lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,1] = lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,1] - phaseFunction(np.radians(PA_degrees),G, ast_hel_dist, g_dist , 'HG') + phaseFunction(np.radians(PA_degrees_norm),G, ast_hel_dist_norm, g_dist_norm , 'HG') + mag_correction

lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,0] = lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,0] - (light_time_correction(g_dist)/(3600*24))
lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,0] = lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,0] - (light_time_correction(g_dist)/(3600*24))

#plot the lightcurve
fig = plt.figure(figsize=((paperwidth*1.15*1.15*1.1) - 2 * margin, (paperheight*1.15) - 2 * margin))
ax1 = fig.add_subplot(111)
line1 = plt.errorbar(x=lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,0], y=lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,1], yerr=lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original[:,2], fmt='s', markerfacecolor = 'r', markeredgecolor = 'black', ecolor='black', capthick=2,markersize=7,label=r'$\mathrm{R, \, 2020/01/14}$',capsize=3)
line2 = plt.errorbar(x=lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,0], y=lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,1], yerr=lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original[:,2], fmt='s', markerfacecolor = 'blue', markeredgecolor = 'black', ecolor='black', capthick=2,markersize=7,label=r'$\mathrm{R, \, 2020/01/15}$',capsize=3)
second_legend = plt.legend(handles=[line1,line2], loc='upper left',prop={'size': 22})
MyFormatter2 = FuncFormatter(latex_ticks2)
ax1.axes.yaxis.set_major_formatter(MyFormatter2)
MyFormatter = FuncFormatter(latex_ticks2)
ax1.axes.xaxis.set_major_formatter(MyFormatter)
#plt.ylim(14,14.5)
plt.title('$\mathrm{2020-01-14,15, MLO\,1.0\, m}$',fontsize=fontsize_standard,y=1.02)
plt.xlabel(r'$\mathrm{Time \, (MJD-UTC)}$',fontsize=fontsize_standard)
plt.ylabel(r'$\mathrm{Normalized \, R \, magnitude}$',fontsize=fontsize_standard)
plt.gca().invert_yaxis()
plt.savefig('2020AV2_MLO_lightcurve_2020_01_14_to_15.pdf')


#lombscargle of MLO data

from astropy.stats import LombScargle

#Test periods combine with periodogram

combined_data_time_mjd_mag_mag_unc = np.concatenate([lightcurve_2020AVA2_2020_01_14_time_mjd_mag_mag_unc_original,lightcurve_2020AVA2_2020_01_15_time_mjd_mag_mag_unc_original],axis=0)

DCTAPO_date_MJD = combined_data_time_mjd_mag_mag_unc[:,0]
DCTAPO_mag, DCTAPO_mag_unc = combined_data_time_mjd_mag_mag_unc[:,1], combined_data_time_mjd_mag_mag_unc[:,2]

plt.figure()
plt.errorbar(x=DCTAPO_date_MJD,y=DCTAPO_mag,yerr=DCTAPO_mag_unc)


fig = plt.figure(figsize=((paperwidth*1.18*1.18*1.5) - 2 * margin, (paperheight*1.25) - 2 * margin))
fig.subplots_adjust(hspace=.35)
ax1 = fig.add_subplot(2,1,1)

minimum_frequency = .25
maximum_frequency=96.
num_peak = 1.0
frequency, power = LombScargle(DCTAPO_date_MJD, DCTAPO_mag, DCTAPO_mag_unc).autopower(samples_per_peak=1000, minimum_frequency = minimum_frequency, maximum_frequency=maximum_frequency)

best_frequency = frequency[np.argmax(power)]/num_peak
phase_fit = np.linspace(0, num_peak)
y_fit = LombScargle(DCTAPO_date_MJD, DCTAPO_mag, DCTAPO_mag_unc).model(t=phase_fit / (best_frequency),
                                    frequency=best_frequency)
phase = (DCTAPO_date_MJD * best_frequency) % 1
ax1.errorbar(phase,  DCTAPO_mag, DCTAPO_mag_unc, fmt='o', mew=0, capsize=0, elinewidth=1.5)


t = np.linspace(0, 1.0,1000.)
Amplitude = (np.max(DCTAPO_mag) - np.min(DCTAPO_mag))*.7
set_phase = np.pi*3.1
y = (Amplitude * 0.5* np.sin(2 * np.pi * t*num_peak + set_phase)) +np.median(DCTAPO_mag)*1.0
ax1.plot(t, y, color='black')
ax1.invert_yaxis()
ax1.set(xlabel=r'$\mathrm{Phase}$', ylabel=r'$\mathrm{Detrended \, F350LP \, magnitude}$')
#plt.title(r'$\mathrm{Phased \; data \;  at \; period:\; '+ str(np.round((1/best_frequency)*24,2))+'\;  h}$')
plt.gca().invert_yaxis()
ax1.set_xlim(0.0,1.0)

ax1 = fig.add_subplot(2,1,2)

frequency, power = LombScargle(DCTAPO_date_MJD, DCTAPO_mag, DCTAPO_mag_unc).autopower(samples_per_peak=1000, minimum_frequency = minimum_frequency, maximum_frequency=maximum_frequency)

best_frequency = frequency[np.argmax(power[np.where(frequency<10)])]/num_peak

line_width = 2.5
mult = 1.2
paperheight = 6.5*1.15
paperwidth = 9.5*1.15
margin = 0.5
ax1.semilogx((1.0/(frequency/num_peak)) *24.,(power),color='grey')
test_period_1=12.
best_frequency1 = 1/test_period_1

ax1.axvline((1.0/(best_frequency)) *24., color='blue', linestyle='-',label =r'$\mathrm{Period:\; '+ str(np.round((1/best_frequency)*24,2))+'\;  h}$',linewidth=2.2)


#ax1.set(ylabel=r'$\mathrm{Power \; level}$', xlabel=r'$\mathrm{Period \; (h)}$')
ax1.set(ylabel=r'$\mathrm{Power}$', xlabel=r'$\mathrm{Lightcurve \; period \; (h)}$')
ax1.set_xlim(0.25,30.0)
ax1.set_ylim(0,1.8)
ax1.legend(loc='upper left',prop={'size':19})

plt.savefig('2020AV2_combined_phased_data_HST_2020_01_14_15_detrended.pdf')
plt.savefig('2020AV2_combined_phased_data_HST_2020_01_14_15_detrended.png')


#statistical significance test

#must run in py36

from astropy.timeseries import LombScargle
import numpy as np

#HST 2019-12-23,24,25 data clean detrended
DCTAPO_date_MJD = np.loadtxt('2I_HST_lightcurve_2019_12_23_24_25_clean_times_mjd.txt')
DCTAPO_mag, DCTAPO_mag_unc = np.loadtxt('2I_HST_lightcurve_2019_12_23_24_25_clean_detrended_mags.txt'), np.loadtxt('2I_HST_lightcurve_2019_12_23_24_25_clean_detrended_mags_unc.txt')

ls = LombScargle(DCTAPO_date_MJD, DCTAPO_mag, DCTAPO_mag_unc)
freq, power = ls.autopower()
print (freq[np.argmax(power)])
print(power.max())
print(ls.false_alarm_probability(power.max()) )


#20200123 LRIS spectra

#comparison with taxonomy

sol_br_wav, sol_br_flux, sol_br_flux_unc = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/LRIS/20200123/lris20200123_2MASS22.spec', usecols=(0,1,3)).T
bor_br_wav_2, bor_br_flux_2, bor_br_flux_unc_2 = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/LRIS/20200123/lris20200123_2020AV2_new.1.spec', usecols=(0,1,3)).T


#target
cap = 1
rebin_factor = 10
flux_blue_x_rebin = rebin_weigted_mean_means_only_1D_array(bor_br_flux_2[cap:],bor_br_flux_unc_2[cap:],rebin_factor)
fluxerr_rebin = rebin_weigted_mean_errors_only_1D_array(bor_br_flux_2[cap:],bor_br_flux_unc_2[cap:],rebin_factor)
bor_br_wav_rebin = bor_br_wav_2[::rebin_factor][cap:]

#standard
cap = 1
flux_std_x_rebin = rebin_weigted_mean_means_only_1D_array(sol_br_flux[cap:],sol_br_flux_unc[cap:],rebin_factor)
stderr_rebin = rebin_weigted_mean_errors_only_1D_array(sol_br_flux[cap:],sol_br_flux_unc[cap:],rebin_factor)
sol_br_wav_rebin = sol_br_wav[::rebin_factor][cap:]

#interpolate rebinned solar flux
solar_blue_rebin_interp = interp1d(sol_br_wav_rebin, flux_std_x_rebin,kind='linear',fill_value='extrapolate')
solar_flux_blue_interp = solar_blue_rebin_interp(bor_br_wav_rebin)

wfinal_microns_rebin_2_2019_01_08 = bor_br_wav_rebin/1e4
flux_ratio_rebin_2_normalize2019_01_08 = flux_blue_x_rebin/solar_flux_blue_interp
flux_ratio_rebin_2_normalize2019_01_08_error = fluxerr_rebin

flux_ratio_rebin_2_normalize2019_01_08 =  flux_ratio_rebin_2_normalize2019_01_08/np.median(flux_ratio_rebin_2_normalize2019_01_08[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]+20])

plt.figure()
B_wavelength_ref_ref_sig = np.loadtxt('bus_demeo_B.txt')
C_wavelength_ref_ref_sig = np.loadtxt('bus_demeo_C.txt')
Q_wavelength_ref_ref_sig = np.loadtxt('bus_demeo_Q.txt')
S_wavelength_ref_ref_sig = np.loadtxt('bus_demeo_S.txt')

x_data = wfinal_microns_rebin_2_2019_01_08
y_data = flux_ratio_rebin_2_normalize2019_01_08
y_data[np.where(np.isnan(y_data)==True)] = np.median(y_data[np.where(np.isnan(y_data)==False)])
y_err = flux_ratio_rebin_2_normalize2019_01_08_error
plot_y_min, plot_y_max = -7, 7
plot_x_min, plot_x_max = 0.45, 1.0
function_lambda = lambda x, a: a*(x-0.55) + 1.0
equation_label = r'$a \; (x \, - \, 0.55) \; + \; 1$'
round_digits = 2
guess_params_numpy_array = np.array([1])

param_labels = np.array(['a','b','c','d','e','f','g','h','i','j','k','l','m','n'])
result = scipy.optimize.curve_fit(function_lambda, x_data, y_data, guess_params_numpy_array, y_err, full_output=True)
pars, corr = result[0], result[1]
chi_sq = np.sum(result[2]['fvec'] ** 2) / (len(result[2]['fvec']) - len(result[0]))

coeff = scipy.stats.pearsonr(x_data, y_data)[0]
pvalue = linear_correlation_coefficient_p_value(coeff, len(x_data), 10000)
deg_freedom = len(x_data) - len(pars)
pvalue_chi_sq = chi_sq_p_value(chi_sq*deg_freedom,deg_freedom,100000,10*chi_sq*deg_freedom)
# 1 standard devation errors of parameters
x_data_range = np.linspace(x_data.min(),x_data.max(),10000)
long_x_range = np.linspace(plot_x_min, x_data_range.max() * 1.1, 10000)
perr = np.sqrt(np.diag(corr))
function_lambda(long_x_range,*pars)
fig = plt.figure(figsize=((paperwidth*1.15*1.15) - 2 * margin, (paperheight*1.15) - 2 * margin))
ax1 = fig.add_subplot(111)


#line6 = plt.fill_between(B_wavelength_ref_ref_sig[:,0], B_wavelength_ref_ref_sig[:,1]-B_wavelength_ref_ref_sig[:,2], B_wavelength_ref_ref_sig[:,1]+B_wavelength_ref_ref_sig[:,2], edgecolor='r', color='r', alpha=0.45,label=r'$\mathrm{B-type}$',zorder=7)
line7 = plt.fill_between(C_wavelength_ref_ref_sig[:,0]*1000., C_wavelength_ref_ref_sig[:,1]-C_wavelength_ref_ref_sig[:,2], C_wavelength_ref_ref_sig[:,1]+C_wavelength_ref_ref_sig[:,2], edgecolor='#FF8F00', color='#FF8F00', alpha=0.35,label=r'$\mathrm{C-type}$',zorder=8)
line8 = plt.fill_between(Q_wavelength_ref_ref_sig[:,0]*1000., Q_wavelength_ref_ref_sig[:,1]-Q_wavelength_ref_ref_sig[:,2], Q_wavelength_ref_ref_sig[:,1]+Q_wavelength_ref_ref_sig[:,2], edgecolor='#B200FF', color='#B200FF', alpha=0.35,label=r'$\mathrm{Q-type}$',zorder=9)
line9 = plt.fill_between(S_wavelength_ref_ref_sig[:,0]*1000., S_wavelength_ref_ref_sig[:,1]-S_wavelength_ref_ref_sig[:,2], S_wavelength_ref_ref_sig[:,1]+S_wavelength_ref_ref_sig[:,2], edgecolor='#009588', color='#009588', alpha=0.35,label=r'$\mathrm{S-type}$',zorder=10)
y_flux = flux_ratio_rebin_2_normalize2019_01_08/function_lambda(wfinal_microns_rebin_2_2019_01_08,*pars)
y_flux[np.where(wfinal_microns_rebin_2_2019_01_08>0.56)] +=0.13
line0 = plt.errorbar(x=wfinal_microns_rebin_2_2019_01_08*1000., y=y_flux, yerr=flux_ratio_rebin_2_normalize2019_01_08_error, fmt='o', markerfacecolor = 'blue', ecolor='blue', capthick=2,label=r'$\mathrm{LRIS, \, MJD \, 58871}$',capsize=3, alpha=0.7,zorder=20)
plt.scatter(0.55, 1.0, color='orange', marker='x',linewidth=3,s=240,zorder=40)

plt.xlim(plot_x_min*1e3, plot_x_max*1e3)
ax1.yaxis.set_major_locator(ticker.MultipleLocator(2*1e3))
ax1.yaxis.set_minor_locator(ticker.MultipleLocator(1*1e3))
ax1.xaxis.set_major_locator(ticker.MultipleLocator(0.1*1e3))
ax1.xaxis.set_minor_locator(ticker.MultipleLocator(0.02*1e3))
MyFormatter2 = FuncFormatter(latex_ticks1)
ax1.axes.yaxis.set_major_formatter(MyFormatter2)
MyFormatter = FuncFormatter(latex_ticks0)
ax1.axes.xaxis.set_major_formatter(MyFormatter)
plt.ylim(0.6,1.4)
plt.yticks(np.arange(0.2,2.0,0.2))
#plt.title('$(2020 \, AV2)$',fontsize=fontsize_standard,y=1.02)
plt.xlabel(r'$\mathrm{Wavelength \, (nm)}$',fontsize=20)
plt.ylabel(r'$\mathrm{Normalied \; Reflectivity}$',fontsize=20)
#plt.plot(long_x_range, function_lambda(long_x_range,*pars), 'black',zorder=55)
lines, labels = ax1.get_legend_handles_labels()
i = 1
starting_position_first_line = 0.955
starting_position_second_line = 0.895
starting_position_third_line = 0.835
starting_position_fourth_line = 0.775
separation = starting_position_first_line - starting_position_second_line
#ax1.text(0.05, starting_position_first_line + 0.1 - (2 * i * separation), equation_label,fontsize=fontsize_standard, transform=ax1.transAxes)
start_string = r'$'
for j in range(0,len(pars)):
    start_string += param_labels[j] + ' \; = ' + str(np.round(pars[j],round_digits))
    start_string += ' \; \pm \; ' + str(np.round(perr[j], 2))
    if j != len(pars)-1:
        start_string += ', \; '
start_string += '$'
#ax1.text(0.05, starting_position_second_line + 0.1 - (2 * i * separation),start_string, fontsize=fontsize_standard,transform=ax1.transAxes)
#ax1.text(0.05, starting_position_third_line + 0.1 - (2 * i * separation),r'$N \; = \; '+str(len(x_data))+ ', \chi^2 \; = \;' + str(np.round(chi_sq, 2)) + ', \; p \; = \; ' + latex_ticks_exp_two_decimal(pvalue_chi_sq,1)[1:-1]+ '$', fontsize=fontsize_standard, transform=ax1.transAxes)
leg = ax1.legend()
for i in range(0,len(leg.legendHandles[:])-2):
    leg.legendHandles[i].set_color('black')
first_legend = ax1.legend(handles=[line9,line8,line7],loc='lower left', prop={'size': 22},numpoints=1,scatterpoints = 1)
ax1 = plt.gca().add_artist(first_legend)
plt.ylim(0.6,1.4)
plt.savefig('2020AV2_LRIS_spectra_for_2020_01_23_and_taxonomy.png')




#20200123 LRIS spectra with error bars

#comparison with taxonomy

lam_factor = 1000.
lam_offset = 5.

sol_br_wav, sol_br_flux, sol_br_flux_unc = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/LRIS/20200123/lris20200123_2MASS22.spec', usecols=(0,1,3)).T
bor_br_wav_2, bor_br_flux_2, bor_br_flux_unc_2 = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/LRIS/20200123/lris20200123_2020AV2_new.1.spec', usecols=(0,1,3)).T


#target
cap = 1
rebin_factor = 10
flux_blue_x_rebin = rebin_weigted_mean_means_only_1D_array(bor_br_flux_2[cap:],bor_br_flux_unc_2[cap:],rebin_factor)
fluxerr_rebin = rebin_weigted_mean_errors_only_1D_array(bor_br_flux_2[cap:],bor_br_flux_unc_2[cap:],rebin_factor)
bor_br_wav_rebin = bor_br_wav_2[::rebin_factor][cap:]

#standard
cap = 1
flux_std_x_rebin = rebin_weigted_mean_means_only_1D_array(sol_br_flux[cap:],sol_br_flux_unc[cap:],rebin_factor)
stderr_rebin = rebin_weigted_mean_errors_only_1D_array(sol_br_flux[cap:],sol_br_flux_unc[cap:],rebin_factor)
sol_br_wav_rebin = sol_br_wav[::rebin_factor][cap:]

#interpolate rebinned solar flux
solar_blue_rebin_interp = interp1d(sol_br_wav_rebin, flux_std_x_rebin,kind='linear',fill_value='extrapolate')
solar_flux_blue_interp = solar_blue_rebin_interp(bor_br_wav_rebin)



wfinal_microns_rebin_2_2019_01_08 = bor_br_wav_rebin/1e4
flux_ratio_rebin_2_normalize2019_01_08_pre = flux_blue_x_rebin/solar_flux_blue_interp
flux_ratio_rebin_2_normalize2019_01_08_error = (flux_blue_x_rebin/solar_flux_blue_interp) * np.sqrt( (fluxerr_rebin/flux_blue_x_rebin)**2 + (np.median(stderr_rebin)/solar_flux_blue_interp)**2)

flux_ratio_rebin_2_normalize2019_01_08 =  flux_ratio_rebin_2_normalize2019_01_08_pre/np.median(flux_ratio_rebin_2_normalize2019_01_08_pre[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]+20])
flux_ratio_rebin_2_normalize2019_01_08_error = flux_ratio_rebin_2_normalize2019_01_08_error/np.median(flux_ratio_rebin_2_normalize2019_01_08_pre[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.55)[0]+20])

#np.median(flux_ratio_rebin_2_normalize2019_01_08_error[np.where(np.isnan(flux_ratio_rebin_2_normalize2019_01_08_error)==False)])

'''
plt.figure()
plt.errorbar(x=wfinal_microns_rebin_2_2019_01_08, y=flux_blue_x_rebin/solar_flux_blue_interp, yerr=flux_ratio_rebin_2_normalize2019_01_08_error, fmt='o', markerfacecolor = 'blue', ecolor='blue', capthick=2)
plt.xlim(0.45, 1.0)

'''

plt.figure()
C_lambda, C_ref, C_ref_sig = np.loadtxt('bus_demeo_C.txt').T
S_lambda, S_ref, S_ref_sig = np.loadtxt('bus_demeo_S.txt').T
Q_lambda, Q_ref, Q_ref_sig = np.loadtxt('bus_demeo_Q.txt').T
V_lambda, V_ref, V_ref_sig = np.loadtxt('bus_demeo_V.txt').T
lunar_wavelength2, lunar_ref2 = np.loadtxt('lunar_spectrum_microns_reflectance_microns_reflectance_2').T
lunar_ref_norm2 = (lunar_ref2/.15) + 0.025

C_low = C_ref-C_ref_sig
C_high = C_ref+C_ref_sig
S_low = S_ref-S_ref_sig
S_high = S_ref+S_ref_sig
Q_low = Q_ref-Q_ref_sig
Q_high = Q_ref+Q_ref_sig
V_low = V_ref-V_ref_sig
V_high = V_ref+V_ref_sig

#1st interp step
C_interp = interp1d(C_lambda,C_ref,kind='cubic',fill_value='extrapolate')
S_interp = interp1d(S_lambda,S_ref,kind='cubic',fill_value='extrapolate')
Q_interp = interp1d(Q_lambda,Q_ref,kind='cubic',fill_value='extrapolate')
V_interp = interp1d(V_lambda,V_ref,kind='cubic',fill_value='extrapolate')
lunar_interp = interp1d(lunar_wavelength2,lunar_ref_norm2,kind='linear',fill_value='extrapolate')

C_low_interp = interp1d(C_lambda,C_low,kind='cubic',fill_value='extrapolate')
S_low_interp = interp1d(S_lambda,S_low,kind='cubic',fill_value='extrapolate')
Q_low_interp = interp1d(Q_lambda,Q_low,kind='cubic',fill_value='extrapolate')
V_low_interp = interp1d(V_lambda,V_low,kind='cubic',fill_value='extrapolate')

C_high_interp = interp1d(C_lambda,C_high,kind='cubic',fill_value='extrapolate')
S_high_interp = interp1d(S_lambda,S_high,kind='cubic',fill_value='extrapolate')
Q_high_interp = interp1d(Q_lambda,Q_high,kind='cubic',fill_value='extrapolate')
V_high_interp = interp1d(V_lambda,V_high,kind='cubic',fill_value='extrapolate')

extend_factor = 0.6
C_lam_micron_fine = np.linspace(C_lambda.min()*extend_factor,C_lambda.max(),1000)
S_lam_micron_fine = np.linspace(S_lambda.min()*extend_factor,S_lambda.max(),1000)
Q_lam_micron_fine = np.linspace(Q_lambda.min()*extend_factor,Q_lambda.max(),1000)
V_lam_micron_fine = np.linspace(V_lambda.min()*extend_factor,V_lambda.max(),1000)
lunar_lam_micron_fine = np.linspace(lunar_ref_norm2.min()*extend_factor,lunar_ref_norm2.max(),1000)

#2nd interp step
C_lam_micron_fine_interp = savgol_filter(C_interp(C_lam_micron_fine),9,1)
S_lam_micron_fine_interp = savgol_filter(S_interp(S_lam_micron_fine),9,1)
Q_lam_micron_fine_interp = savgol_filter(Q_interp(Q_lam_micron_fine),9,1)
V_lam_micron_fine_interp = savgol_filter(V_interp(V_lam_micron_fine),9,1)
lunar_lam_micron_fine_interp = savgol_filter(lunar_interp(lunar_lam_micron_fine),29,1)

C_low_lam_micron_fine_interp = savgol_filter(C_low_interp(C_lam_micron_fine),9,1)
S_low_lam_micron_fine_interp = savgol_filter(S_low_interp(S_lam_micron_fine),9,1)
Q_low_lam_micron_fine_interp = savgol_filter(Q_low_interp(Q_lam_micron_fine),9,1)
V_low_lam_micron_fine_interp = savgol_filter(V_low_interp(V_lam_micron_fine),9,1)

C_high_lam_micron_fine_interp = savgol_filter(C_high_interp(C_lam_micron_fine),9,1)
S_high_lam_micron_fine_interp = savgol_filter(S_high_interp(S_lam_micron_fine),9,1)
Q_high_lam_micron_fine_interp = savgol_filter(Q_high_interp(Q_lam_micron_fine),9,1)
V_high_lam_micron_fine_interp = savgol_filter(V_high_interp(V_lam_micron_fine),9,1)

x_data = wfinal_microns_rebin_2_2019_01_08
y_data = flux_ratio_rebin_2_normalize2019_01_08
y_data[np.where(np.isnan(y_data)==True)] = np.median(y_data[np.where(np.isnan(y_data)==False)])
y_err = flux_ratio_rebin_2_normalize2019_01_08_error
plot_y_min, plot_y_max = -7, 7
plot_x_min, plot_x_max = 0.45, 1.0
function_lambda = lambda x, a: a*(x-0.55) + 1.0
equation_label = r'$a \; (x \, - \, 0.55) \; + \; 1$'
round_digits = 2
guess_params_numpy_array = np.array([1])

param_labels = np.array(['a','b','c','d','e','f','g','h','i','j','k','l','m','n'])
result = scipy.optimize.curve_fit(function_lambda, x_data, y_data, guess_params_numpy_array, fluxerr_rebin, full_output=True)
pars, corr = result[0], result[1]
chi_sq = np.sum(result[2]['fvec'] ** 2) / (len(result[2]['fvec']) - len(result[0]))

coeff = scipy.stats.pearsonr(x_data, y_data)[0]
pvalue = linear_correlation_coefficient_p_value(coeff, len(x_data), 10000)
deg_freedom = len(x_data) - len(pars)
pvalue_chi_sq = chi_sq_p_value(chi_sq*deg_freedom,deg_freedom,100000,10*chi_sq*deg_freedom)
# 1 standard devation errors of parameters
x_data_range = np.linspace(x_data.min(),x_data.max(),10000)
long_x_range = np.linspace(plot_x_min, x_data_range.max() * 1.1, 10000)
perr = np.sqrt(np.diag(corr))
function_lambda(long_x_range,*pars)
fig = plt.figure(figsize=((paperwidth*1.15*1.15) - 2 * margin, (paperheight*1.15) - 2 * margin))
ax1 = fig.add_subplot(111)
line7 = plt.fill_between(C_lam_micron_fine*lam_factor, C_low_lam_micron_fine_interp, C_high_lam_micron_fine_interp, edgecolor='#B200FF', color='#B200FF', alpha=0.8,label=r'$\mathrm{C-type}$',zorder=7)
line8 = plt.fill_between(S_lam_micron_fine*lam_factor, S_low_lam_micron_fine_interp, S_high_lam_micron_fine_interp, edgecolor='#FF8F00', color='#FF8F00', alpha=0.8,label=r'$\mathrm{S-type}$',zorder=7)
line9 = plt.fill_between(V_lam_micron_fine*lam_factor, V_low_lam_micron_fine_interp, V_high_lam_micron_fine_interp, edgecolor='#009588', color='#009588', alpha=0.8,label=r'$\mathrm{V-type}$',zorder=7)

y_flux = flux_ratio_rebin_2_normalize2019_01_08/function_lambda(wfinal_microns_rebin_2_2019_01_08,*pars)
y_flux[np.where(wfinal_microns_rebin_2_2019_01_08>0.56)] +=0.13
line0 = plt.errorbar(x=wfinal_microns_rebin_2_2019_01_08*1000., y=y_flux, yerr=flux_ratio_rebin_2_normalize2019_01_08_error, fmt='o', markerfacecolor = 'blue', ecolor='blue', capthick=2,label=r'$\mathrm{LRIS, \, MJD \, 58871}$',capsize=3, alpha=0.9,zorder=20)
plt.scatter(550, 1.0, color='black', marker='x',linewidth=6,s=800,zorder=40)

plt.xlim(plot_x_min*1e3, plot_x_max*1e3)
ax1.yaxis.set_major_locator(ticker.MultipleLocator(2*1e3))
ax1.yaxis.set_minor_locator(ticker.MultipleLocator(1*1e3))
ax1.xaxis.set_major_locator(ticker.MultipleLocator(0.1*1e3))
ax1.xaxis.set_minor_locator(ticker.MultipleLocator(0.02*1e3))
MyFormatter2 = FuncFormatter(latex_ticks1)
ax1.axes.yaxis.set_major_formatter(MyFormatter2)
MyFormatter = FuncFormatter(latex_ticks0)
ax1.axes.xaxis.set_major_formatter(MyFormatter)
plt.ylim(0.6,1.4)
plt.yticks(np.arange(0.2,2.0,0.2))
plt.xlabel(r'$\mathrm{Wavelength \, (nm)}$',fontsize=28)
plt.ylabel(r'$\mathrm{Normalized \; reflectivity}$',fontsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
lines, labels = ax1.get_legend_handles_labels()
i = 1
starting_position_first_line = 0.955
starting_position_second_line = 0.895
starting_position_third_line = 0.835
starting_position_fourth_line = 0.775
separation = starting_position_first_line - starting_position_second_line
#ax1.text(0.05, starting_position_first_line + 0.1 - (2 * i * separation), equation_label,fontsize=fontsize_standard, transform=ax1.transAxes)
start_string = r'$'
for j in range(0,len(pars)):
    start_string += param_labels[j] + ' \; = ' + str(np.round(pars[j],round_digits))
    start_string += ' \; \pm \; ' + str(np.round(perr[j], 2))
    if j != len(pars)-1:
        start_string += ', \; '
start_string += '$'
leg = ax1.legend()
for i in range(0,len(leg.legendHandles[:])-2):
    leg.legendHandles[i].set_color('black')
first_legend = ax1.legend(handles=[line9,line8,line7],loc='lower left', prop={'size': 22},numpoints=1,scatterpoints = 1)
ax1 = plt.gca().add_artist(first_legend)
plt.ylim(0.6,1.4)
plt.savefig('2020AV2_LRIS_spectra_for_2020_01_23_and_taxonomy.pdf')


print "#wavelength_nm_0 reflectivity_1 reflectivity_1_sigma_uncertainty_2"
for i in range(0,len(wfinal_microns_rebin_2_2019_01_08)):
    print wfinal_microns_rebin_2_2019_01_08[i]*1000.,y_flux[i],flux_ratio_rebin_2_normalize2019_01_08_error[i]

#spectral slope between g,r,i,, g-r, r-i i-z colors

#calculate B,R and Z colors of 2020 CD3
g_av = np.median( y_flux[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.477)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.477)[0]+20])
r_av = np.median( y_flux[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.623)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.623)[0]+20])
i_av = np.median( y_flux[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.763)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.763)[0]+20])
z_av = np.median( y_flux[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.913)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.913)[0]+20])

np.median(flux_ratio_rebin_2_normalize2019_01_08_error[find_nearest(wfinal_microns_rebin_2_2019_01_08,0.477)[0]-20:find_nearest(wfinal_microns_rebin_2_2019_01_08,0.477)[0]+20])

#                               solar_spectrum
g_r_av = asteroid_colors_from_spectrum(g_av,r_av,0.44)
r_i_av = asteroid_colors_from_spectrum(r_av,i_av,0.11)
i_z_av = asteroid_colors_from_spectrum(i_av,z_av,0.03)
r_z_av = r_i_av + i_z_av
g_i_av = g_r_av + r_i_av

g_i_slope = normalized_reflectance_spectral_slope(g_i_av, 0.55, 0.477, 0.763)



#objectsInField code notes
'''
objectsInField-master

#different runs
survey_simulation_objects_in_field_standard_install/objectsInField-master #original run with G = 0.15, also C-type (default value)
survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_10 #D type G = 0.10
survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_20 #X type G = 0.20
survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_25 #S/Q type G = 0.25
survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_45 #E type G = 0.45


your .db database file must in in the following format and

observationId', 'ra', 'dec', 'observationStartMJD', 'angle'

  58849     101.45719147      23.02131081   58849.41796875   0.
  58850     102.52411652      22.93985939   58850.41796875   0.
  58851     103.58973694      22.85128593   58851.41796875   0.
  58852     104.65387726      22.75563049   58852.41796875   0.
  58853     105.71649170      22.65293312   58853.41796875   0.
  58854     106.77741241      22.54324532   58854.41796875   0.

and have the table name ObsHistory


To fix frank's file run:

awk '{printf "%s %.7f %.7f %.7f %s'\\n'",$'10',$'5',$'6',$'4'-2400000.500000,"0.0"}' < All_twilight_obs.txt >twilight_since_2019_09_20.txt

tail -r twilight_since_2019_09_20.txt > twilight_since_2019_09_20_reverse.txt

your database file must in in the following format

observationId ra dec observationStartMJD angle

termsql -c observationId,ra,dec,observationStartMJD,angle -i twilight_since_2019_09_20_reverse.txt -o twilight_since_2019_09_20.db -t ObsHistory

test .db file with 

import sqlite3
conn = sqlite3.connect('twilight_since_2019_09_20.db')
cur = conn.cursor()
#you need to know the table name, in this case it is "ObsHistory"
query = conn.execute("SELECT * From ObsHistory")
cols = [column[0] for column in query.description]
print (cols)

import pandas as pd
#load into a pandas dataframe
results= pd.DataFrame.from_records(data = query.fetchall(), columns = cols)

import sqlite3
import pandas as pd
# Create your connection.
cnx = sqlite3.connect('twilight_since_2019_09_20.db')

df = pd.read_sql_query("SELECT * FROM ObsHistory", cnx)


#updated Twilight survey pointings to 2020 07 08

awk '{printf "%s %.7f %.7f %.7f %s'\\n'",$'10',$'5',$'6',$'4'-2400000.500000,"0.0"}' < twilight_obs_07.08.2020.txt >twilight_since_updated_to_2020_07_08.txt

tail -r twilight_since_updated_to_2020_07_08.txt > twilight_since_updated_to_2020_07_08_reverse.txt

your database file must in in the following format

observationId ra dec observationStartMJD angle

termsql -c observationId,ra,dec,observationStartMJD,angle -i twilight_since_updated_to_2020_07_08_reverse.txt -o twilight_since_2020_07_08.db -t ObsHistory

test .db file with 

import sqlite3
conn = sqlite3.connect('twilight_since_2020_07_08.db')
cur = conn.cursor()
#you need to know the table name, in this case it is "ObsHistory"
query = conn.execute("SELECT * From ObsHistory")
cols = [column[0] for column in query.description]
print (cols)

import pandas as pd
#load into a pandas dataframe
results= pd.DataFrame.from_records(data = query.fetchall(), columns = cols)

import sqlite3
import pandas as pd
# Create your connection.
cnx = sqlite3.connect('twilight_since_2020_07_08.db')

df = pd.read_sql_query("SELECT * FROM ObsHistory", cnx)


#updated Twilight survey pointings to 2019 09 20 to 2020 07 08

awk '{printf "%s %.7f %.7f %.7f %s'\\n'",$'10',$'5',$'6',$'4'-2400000.500000,"0.0"}' < twilight_obs_09.20.2019_to_07.08.2020.txt > twilight_since_updated_2019_09_20_to_2020_07_08.txt

tail -r twilight_since_updated_2019_09_20_to_2020_07_08.txt > twilight_since_updated_2019_09_20_to_2020_07_08_reverse.txt

your database file must in in the following format

observationId ra dec observationStartMJD angle

termsql -c observationId,ra,dec,observationStartMJD,angle -i twilight_since_updated_2019_09_20_to_2020_07_08_reverse.txt -o twilight_since_2019_09_20_to_2020_07_08.db -t ObsHistory

test .db file with 

import sqlite3
conn = sqlite3.connect('twilight_since_2019_09_20_to_2020_07_08.db')
cur = conn.cursor()
#you need to know the table name, in this case it is "ObsHistory"
query = conn.execute("SELECT * From ObsHistory")
cols = [column[0] for column in query.description]
print (cols)

import pandas as pd
#load into a pandas dataframe
results= pd.DataFrame.from_records(data = query.fetchall(), columns = cols)

import sqlite3
import pandas as pd
# Create your connection.
cnx = sqlite3.connect('twilight_since_2019_09_20_to_2020_07_08.db')

df = pd.read_sql_query("SELECT * FROM ObsHistory", cnx)



#updated Twilight survey pointings to 2019 09 20 to 2020 01 30

awk '{printf "%s %.7f %.7f %.7f %s'\\n'",$'10',$'5',$'6',$'4'-2400000.500000,"0.0"}' < twilight_obs_09.20.2019_to_01.30.2020.txt > twilight_since_updated_2019_09_20_to_2020_01_30.txt

tail -r twilight_since_updated_2019_09_20_to_2020_01_30.txt > twilight_since_updated_2019_09_20_to_2020_01_30_reverse.txt

your database file must in in the following format

observationId ra dec observationStartMJD angle

termsql -c observationId,ra,dec,observationStartMJD,angle -i twilight_since_updated_2019_09_20_to_2020_01_30_reverse.txt -o twilight_since_updated_2019_09_20_to_2020_01_30.db -t ObsHistory

#test .db file with 

import sqlite3
conn = sqlite3.connect('twilight_since_updated_2019_09_20_to_2020_01_30.db')
cur = conn.cursor()
#you need to know the table name, in this case it is "ObsHistory"
query = conn.execute("SELECT * From ObsHistory")
cols = [column[0] for column in query.description]
print (cols)

import pandas as pd
#load into a pandas dataframe
results= pd.DataFrame.from_records(data = query.fetchall(), columns = cols)

import sqlite3
import pandas as pd
# Create your connection.
cnx = sqlite3.connect('twilight_since_updated_2019_09_20_to_2020_01_30.db')

df = pd.read_sql_query("SELECT * FROM ObsHistory", cnx)



Fix Mikael's file here synthetic_vatiras_1000x.txt

a,e,i,Omega,omega, M, H

  0.543964386      0.258553058       46.1263924       31.2065144       246.172775       180.478546       17.4867764    
  
Need to put format in .des format

!!OID FORMAT a e inc Omega argPeri meanAnomaly H t_0 INDEX N_PAR MOID COMPCODE
isru1 KEP 1.021489281928 0.19544556093290 1.398188196278 258.200691184349 10.190284437679 137.189483775072 0.0 57653.83506900000066 1 6 -1 JEDICKE
isru2 KEP 1.014136720395 0.05264307480560 4.573753019671 190.791982566478 90.130885750184 316.766674084682 0.0 57653.83506900000066 1 6 -1 JEDICKE
isru3 KEP 0.971535555279 0.16348096599005 2.385619309529 206.457968153881 147.333164686566 19.931655783192 0.0 57653.83506900000066 1 6 -1 JEDICKE

# need to use 58437.0 as the epoch for all

and vat as the prefix, so

vat1  0.543964386      0.258553058       46.1263924       31.2065144       246.172775       180.478546       17.4867764     58437.0 1 6 -1 JEDICKE

To fix Mikael's file, run

awk '{print "KEP", $'1', $'2', $'3',$'4',$'5',$'6',$'7',"58437.0 1 6 -1 JEDICKE"}' < synthetic_vatiras_1000x.txt | awk '{printf("%04d %s\n", NR, $0)}'| sed 's/^/vat/' > granvik_2018_vatiras.des

#example

vat2501 KEP 0.535217345 0.258732945 23.6390018 207.556519 337.094727 210.395248 17.8840237 58437.0 1 6 -1 JEDICKE
vat2502 KEP 0.455606282 0.466937393 48.4320869 231.588562 329.908630 15.8395281 16.9953003 58437.0 1 6 -1 JEDICKE
vat2503 KEP 0.538086772 0.301169097 18.9057064 46.9668541 239.723343 359.717621 17.4888840 58437.0 1 6 -1 JEDICKE
vat2504 KEP 0.453293264 0.422379941 17.2886028 225.588379 118.550850 247.568024 17.0741024 58437.0 1 6 -1 JEDICKE


#to fix morby's files
#vatiras
#in morby_neo_file_fixer.py

import numpy as np
import random
test_a, test_e, test_i, test_H, test_alb = np.loadtxt('synthetic-Vatiras.dat',dtype='float').T

for i in range(0,len(test_a)):
 print "ast"+"%08d" % (i,), "KEP", test_a[i], test_e[i], test_i[i], random.uniform(0.0, 360.0), random.uniform(0.0, 360.0), random.uniform(0.0, 360.0), test_H[i], '58437.0 1 6 -1 JEDICKE'
 
 python morby_neo_file_fixer.py > morby_synthetic_Vatiras.des


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
MUST PUT !!!!!!!

OID FORMAT a e inc Omega argPeri meanAnomaly H t_0 INDEX N_PAR MOID COMPCODE 

AT THE TOP OF DES FILES OR RUNS WILL BE CORRUPTED!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

run objectsInField in

/Users/bolin/NEO/Follow_up/APO_observing/survey_simulation_objects_in_field_standard_install/objectsInField-master


config file: isru-10circle.config
you must run the code first with Make SPKs = T
Then run again with Make SPKs = F

Also must set this line to the number of your fields
; Number of fields to read from Survey Simulation (int)                                                                                                                                             
nFields             = 7551

#mean phase function for all asteroid taxonomic types
must run in python 3.6
py36
python -i main.py isru-10circle.config
Change Make SPKs to T
must erase contents of data/spk/ everytime you have a new population model
Then change back to F for every subsequent run

survey_simulation_objects_in_field_standard_install/objectsInField-master #original run with G = 0.15, also C-type (default value)
python main.py isru-10circle.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations
#get names of objects detected
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations | sort -u > vatiras_detected_names

awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected.des

check check to remove objects moving faster than 2 degs/day

awk '{ if (($7^2 + $9^2)^.5<2.) { print } }' twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_15



modify the slope parameter on lines 129 to 130 in code/orbits.py :

        if 'g' not in orbits:
            orbits['g'] = np.zeros(nSso) + 0.15
            
            
survey_simulation_objects_in_field_standard_install/objectsInField-master #original run with G = 0.15, also C-type (default value)

#survey_simulation_objects_in_field_standard_install/objectsInField-master_test #D type G = 0.15
python main.py isru-10circle.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_test
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_test | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected_test.des

#survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_10 #D type G = 0.10
python main.py isru-10circle.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_10
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_10 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected_G_0_10.des

#survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_20 #X type G = 0.20
python main.py isru-10circle.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_20
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_20 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected_G_0_20.des

#survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_25 #S/Q type G = 0.25
python main.py isru-10circle.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_25
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_25 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected_G_0_25.des

#survey_simulation_objects_in_field_standard_install/objectsInField-master_G_0_45 #E type G = 0.45
python main.py isru-10circle.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_45
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations_G_0_45 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > granvik_2018_vatiras_detected_G_0_45.des


#Morby vatira runs
#15<H<21

Test using the old survey db to 2020 1 30

#G 0.15 morby test pointings only to 2020 01 30, H < 18
#/Users/bolin/NEO/Follow_up/APO_observing /survey_simulation_objects_in_field_standard_install/objectsInField-master_test_morby
python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_01_31_observations_G_0_15
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_01_31_observations_G_0_15 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_15.des

#G 0.15 granvik test new pointings to 2020 01 31
#/Users/bolin/NEO/Follow_up/APO_observing/survey_simulation_objects_in_field_standard_install/objectsInField-master_test_granvik
python main.py isru-10circle_morby_vatira.config > twilight_survey_vatiras_detected_2019_09_20_to_2020_01_31_observations_G_0_15
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_01_31_observations_G_0_15 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names granvik_2018_vatiras.des > morby_synthetic_Vatiras_detected_G_0_15.des


#G 0.15
#/Users/bolin/NEO/Follow_up/APO_observing/survey_simulation_objects_in_field_standard_install/objectsInField-master
python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_15
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_15 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_15.des

#G 0.10
python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_10
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_10 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_10.des

#G 0.20
python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_10
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_20 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_10.des


#G 0.25
python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_25
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_25 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_25.des

#G 0.45

python main.py isru-10circle_morby_vatira.config -f > twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_45
awk '{print $'1'}' < twilight_survey_vatiras_detected_2019_09_20_to_2020_07_08_observations_G_0_45 | sort -u > vatiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' vatiras_detected_names morby_synthetic_Vatiras.des > morby_synthetic_Vatiras_detected_G_0_45.des


#Morby Atira runs
#15<H<21
#G 0.15
#/Users/bolin/NEO/Follow_up/APO_observing/survey_simulation_objects_in_field_standard_install/objectsInField-master
python main.py isru-10circle_morby_atira.config -f > twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_15
awk '{print $'1'}' < twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_15 | sort -u > atiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' atiras_detected_names morby_synthetic_Atiras.des > morby_synthetic_Atiras_detected_G_0_15.des

#G 0.10
python main.py isru-10circle_morby_atira.config > twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_10
awk '{print $'1'}' < twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_10 | sort -u > atiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' atiras_detected_names morby_synthetic_Atiras.des > morby_synthetic_Atiras_detected_G_0_10.des

#G 0.20
python main.py isru-10circle_morby_atira.config > twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_20
awk '{print $'1'}' < twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_20 | sort -u > atiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' atiras_detected_names morby_synthetic_Atiras.des > morby_synthetic_Atiras_detected_G_0_20.des


#G 0.25
python main.py isru-10circle_morby_atira.config > twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_25
awk '{print $'1'}' < twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_25 | sort -u > atiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' atiras_detected_names morby_synthetic_Atiras.des > morby_synthetic_Atiras_detected_G_0_25.des

#G 0.45

python main.py isru-10circle_morby_atira.config > twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_45
awk '{print $'1'}' < twilight_survey_atiras_detected_2019_09_20_to_2020_07_08_observations_G_0_45 | sort -u > atiras_detected_names
awk 'NR==FNR{_[$1];next}$1 in _' atiras_detected_names morby_synthetic_Atiras.des > morby_synthetic_Atiras_detected_G_0_45.des

'''

#survey simulator stats


#found objects observations

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T


#break in field MJDs is58746, everything before MJD 58746 is the old twilight survey. New twilight survey began on 2020 09 20



#positions of sun
from astroquery.jplhorizons import Horizons
print(Horizons(id='10', id_type='id').ephemerides())


#telling angle between object and sun
from astropy.coordinates import SkyCoord  # High-level coordinates
from astroquery.jplhorizons import Horizons
import numpy as np
import argparse



def angles_in_degrees_zeroTo360_out_degrees(val):
    while val < 0:
        val += 360.
    while val > 360.:
        val -= 360.
    return val

object_name='10'
time_of_obs_JD = 2458863.7987
obj_ra_deg, obj_dec_deg = 56.992561, 38.442176

#get sun ecliptic lon, lat
time_of_obs = np.round(time_of_obs_JD,3)# round to 3 due to weird rounding behavior in ephemerides class. Shouldn't final affect angular distance measurement
eph_sun = Horizons(id='10', id_type='id',epochs=time_of_obs).ephemerides()#10 is the JPL Horizons ID for the sun
pos_sun = SkyCoord(np.radians(eph_sun['RA']), np.radians(eph_sun['DEC']),unit='rad').geocentrictrueecliptic[0]
sun_ec_lon_deg, sun_ec_lat_deg = pos_sun.lon.deg,pos_sun.lat.deg

oppos_ec_lon_deg, oppos_ec_lat_deg = angles_in_degrees_zeroTo360_out_degrees(sun_ec_lon_deg-180.),sun_ec_lat_deg*-1.0

pos_opp = SkyCoord(oppos_ec_lon_deg, oppos_ec_lat_deg, unit='deg')

#get object coords, convert to ecliptic lon, lat
pos_obj = SkyCoord(np.radians(obj_ra_deg), np.radians(obj_dec_deg),unit='rad').geocentrictrueecliptic

print pos_obj.separation(pos_opp).deg, pos_obj.lat.deg


#survey residence plots

#fields
fov_size = 3.7081 * 2

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1).T

#all_field_mjd[-1]=58724.0
#all_field_ra_deg[-1]=126.16770
#all_field_dec_deg[-1]=33.93873

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(all_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(all_field_ra_deg))
    pointing_date = all_field_mjd[j]
    pointing_ra_rad = np.radians(all_field_ra_deg[j])
    pointing_dec_rad = np.radians(all_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
plt.imshow(n_grid[::-1,::-1], extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric Ecliptic Longitude [deg]",fontsize="30")
plt.ylabel("Sun-centric Ecliptic Latitude [deg]",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(-70,70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))


#cbar fontsize change
cb = plt.colorbar() # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of Visits',fontsize="30")
plt.savefig('vatira_survey_all.pdf')

#twilight survey fields new survey
#fields
fov_size = 3.7081 * 2

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1).T


only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1)[np.where(all_field_mjd>58742)].T

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd[j]
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
plt.imshow(n_grid[::-1,::-1], extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric Ecliptic Longitude [deg]",fontsize="30")
plt.ylabel("Sun-centric Ecliptic Latitude [deg]",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(-70,70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))


#cbar fontsize change
cb = plt.colorbar() # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of Visits',fontsize="30")
plt.savefig('vatira_survey_only_new_survey.pdf')


#survey residence plots for fields that contained ivos

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1).T

all_field_mjd_round_6 = all_field_mjd.round(6)
found_FieldMJD_round_6 = found_FieldMJD.round(6)

in_field_mjd, in_field_ra_deg, in_field_dec_deg = np.array([]), np.array([]), np.array([])

count =0
unique_found_FieldID = np.unique(found_FieldID)
for i in range(0,len(unique_found_FieldID)):
    print i
    unique_found_FieldMJD = np.unique(found_FieldMJD_round_6[np.where(found_FieldID==unique_found_FieldID[i])])
    for j in range(0,len(unique_found_FieldMJD)):
        #print j
        index = np.where((all_field_id==unique_found_FieldID[i])& (all_field_mjd_round_6==unique_found_FieldMJD[j]))[0]
        if len(index)>0:
            #print index, len(index), all_field_mjd[index], all_field_ra_deg[index], all_field_dec_deg[index]
            in_field_mjd, in_field_ra_deg, in_field_dec_deg = np.append(in_field_mjd,all_field_mjd[index]), np.append(in_field_ra_deg,all_field_ra_deg[index]), np.append(in_field_dec_deg,all_field_dec_deg[index])



#fields
fov_size = 3.7081 * 2

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(in_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(in_field_ra_deg))
    pointing_date = in_field_mjd[j]
    pointing_ra_rad = np.radians(in_field_ra_deg[j])
    pointing_dec_rad = np.radians(in_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,:], extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(-70,70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))
#cbar fontsize change
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.15)
cb = plt.colorbar(im, cax=cax) # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
plt.savefig('fields_containing_ivos.png')


# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.





#survey residence plots for fields that contained ivos for new survey only

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1).T

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg  = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1)[np.where(all_field_mjd>58742)].T

all_field_mjd_round_6 = all_field_mjd.round(6)
found_FieldMJD_round_6 = found_FieldMJD.round(6)

in_field_mjd, in_field_ra_deg, in_field_dec_deg = np.array([]), np.array([]), np.array([])

count =0
unique_found_FieldID = np.unique(found_FieldID)
for i in range(0,len(unique_found_FieldID)):
    print i
    unique_found_FieldMJD = np.unique(found_FieldMJD_round_6[np.where(found_FieldID==unique_found_FieldID[i])])
    for j in range(0,len(unique_found_FieldMJD)):
        #print j
        index = np.where((all_field_id==unique_found_FieldID[i])& (all_field_mjd_round_6==unique_found_FieldMJD[j]))[0]
        if len(index)>0:
            #print index, len(index), all_field_mjd[index], all_field_ra_deg[index], all_field_dec_deg[index]
            in_field_mjd, in_field_ra_deg, in_field_dec_deg = np.append(in_field_mjd,all_field_mjd[index]), np.append(in_field_ra_deg,all_field_ra_deg[index]), np.append(in_field_dec_deg,all_field_dec_deg[index])



#fields
fov_size = 3.7081 * 2

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(in_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(in_field_ra_deg))
    pointing_date = in_field_mjd[j]
    pointing_ra_rad = np.radians(in_field_ra_deg[j])
    pointing_dec_rad = np.radians(in_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
plt.imshow(n_grid[::-1,::-1], extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric Ecliptic Longitude [deg]",fontsize="30")
plt.ylabel("Sun-centric Ecliptic Latitude [deg]",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(-70,70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))


#cbar fontsize change
cb = plt.colorbar() # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of Visits',fontsize="30")
plt.savefig('fields_containing_ivos_only_new_survey.pdf')


#efficiency calculation

'''

Frank's efficiency calcualtion
if(mjds[$i]<=58657.4705095){efficiency=0.72/(1 + exp((vmag[$i] - 19.52 )/0.4));} else {efficiency=0.87/(1 + exp((vmag[$i] - 20.6)/0.735));}


'''

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_mjd, all_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1).T


#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T

'''
Twilight survey phases:
Phase 1: (2018, 11, 15, 0.0, 0) to (2019, 2, 8, 0.0, 0)
Phase 2: (2019, 3, 30, 0.0, 0) to (2019, 6, 21, 0.11262729999725707, 0)
Phase 3: (2019, 9, 19, 0.0, 0) to present
'''


#orbital element histograms


#found objects observations

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[index] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[index]]).T

#official elements
a_off = 0.55536
e_off = 0.177296
i_off = 15.880
H_off = 16.3
first_note = 'Synthetic population'
second_note = 'Synthetic population detected'
notes = np.array([first_note,second_note])
plt_dir='./'
ext = '.png'
#ytick_locs = np.arange(0,1.2,0.1)


#a hist
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'a (au)'
file_name='synth_a_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#e hist
a_hist_bins_min = 0.0
a_hist_bins_max = 1.05
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = e_off
xlim_min,xlim_max = 0.0,1.0

x_label = 'e'
file_name='synth_e_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_e,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#i hist
a_hist_bins_min = 0.0
a_hist_bins_max = 85
delta_a_hist_bins = 5
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = i_off
xlim_min,xlim_max = 0.0,85

x_label = 'i (deg)'
file_name='synth_i_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_i_deg,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_i_deg,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.05
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H Magnitude'
file_name='synth_H_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#orbital element histograms with uncertainties


#found objects observations

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[i] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[i]]).T

#official elements
a_off = 0.55536
e_off = 0.177296
i_off = 15.880
H_off = 16.4
first_note = 'Synthetic population'
second_note = 'Synthetic population detected'
notes = np.array([first_note,second_note])
plt_dir='./'
ext = '.png'
#ytick_locs = np.arange(0,1.2,0.1)


#a hist
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'a (au)'
file_name='synth_a_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins)) + make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/((-1.0 *np.sqrt(make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))) + make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties_alternative(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#e hist
a_hist_bins_min = 0.0
a_hist_bins_max = 1.05
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = e_off
xlim_min,xlim_max = 0.0,1.0

x_label = 'e'
file_name='synth_e_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_e,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_e,bins)) + make_histogram1d(vat_all_e,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_e,bins)) + make_histogram1d(vat_all_e,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#i hist
a_hist_bins_min = 0.0
a_hist_bins_max = 85
delta_a_hist_bins = 5
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = i_off
xlim_min,xlim_max = 0.0,85

x_label = 'i (deg)'
file_name='synth_i_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_i_deg,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_i_deg,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_i_deg,bins)) + make_histogram1d(vat_all_i_deg,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_i_deg,bins)) + make_histogram1d(vat_all_i_deg,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#Q hist
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off * (1+e_off)
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'Q (au)'
file_name='synth_Q_detected_with_errors_v2'
xtick_locs = a_hist_bins

vat_all_Q_au = (1.+vat_all_e)*vat_all_a_au

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/((-1.0 *np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])


xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max = xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max

fig = plt.figure(figsize=(12 - 2*margin, 10 - 2*margin))
ax1 = fig.add_subplot(111)
plt.fill_between(bins,data_sets[0], step="pre", alpha=0.4,color='grey')
plt.plot(bins[1:],data_sets[0][1:], drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.4)
plt.xlabel(x_label,fontsize=28)
plt.ylabel('Number of synthetic objects',fontsize=28)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
ax3 = ax1.twiny()
ax3.set_xlabel('Maximum Solar Elongation (degrees)',fontsize=26)
#max_solar_elong_deg = np.round(np.degrees(np.arcsin(bins/1.)),1)
max_solar_elong_deg = np.arange(20.5,55.5,3.5)
bins2=  np.arange(.35,.825,.05)
bins2[1:] +=0.025
plt.plot(max_solar_elong_deg,bins2, drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.0)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
ax2.set_ylabel('Completeness',fontsize=28)  # we already handled the x-label with ax1
#plt.step(np.append(np.append(bins[np.where(data_sets[1]>0.0)][0], bins[np.where(data_sets[1]>0.0)]),bins[np.where(data_sets[1]>0.0)][-1]), np.append(0.0,np.append(data_sets[1][np.where(data_sets[1]>0.0)],0.0)),linewidth=2.5*1.25,label = r'$\mathrm{' + notes[1] +'}$',color='red')
plt.step(bins[1:14], data_sets[1][1:14],linewidth=2.5*1.25,label = r'$\mathrm{' + notes[1] +'}$',color='red')
error_bar_high = data_sets[3] - data_sets[1]
error_bar_low = data_sets[1] - data_sets[2]
plt.errorbar(bins[1:][np.where(data_sets[1][1:]>0.0)]-(np.diff(bins)[0]/2.), data_sets[1][1:][np.where(data_sets[1][1:]>0.0)], yerr=[error_bar_low[1:][np.where(data_sets[1][1:]>0.0)],error_bar_high[1:][np.where(data_sets[1][1:]>0.0)]],color='red',linewidth=2.5*1.25,fmt='o',capsize=5*1.25, elinewidth=2*1.25,markeredgewidth=2*1.25,ms=0.1)
plt.axvline(vline_loc, color='black',linestyle='--',linewidth=5)
#ax1.set_yticks(ytick_locs)
ax1.set_xticks(xtick_locs[::2])
ax1.set_xlim(xlim_min,xlim_max)
ax3.set_xticks(max_solar_elong_deg[::2]+1)
ax3.set_xticklabels(max_solar_elong_deg[::2].astype('string'))
MyFormatter2 = FuncFormatter(latex_ticks1)
ax3.axes.xaxis.set_major_formatter(MyFormatter2)
ax3.set_xlim(max_solar_elong_deg.min(),max_solar_elong_deg.max())
ax1.set_ylim(0.0,(data_sets[0].max()*1.0)*1.25)
#ax2.set_ylim(0.0,(data_sets[3].max()*1.0)*1.25)
ax2.set_ylim(0.0,0.6)
plt.tick_params(labelsize=28)
fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
plt.savefig(plt_dir + file_name + ext)

#Q dist, no completeness, no vline
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off * (1+e_off)
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'Q (au)'
file_name='synth_Q_IVO'
xtick_locs = a_hist_bins

vat_all_Q_au = (1.+vat_all_e)*vat_all_a_au

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/((-1.0 *np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])


xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max = xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max

fig = plt.figure(figsize=(12 - 2*margin, 10 - 2*margin))
ax1 = fig.add_subplot(111)
plt.fill_between(bins,data_sets[0], step="pre", alpha=0.4,color='grey')
plt.plot(bins[1:],data_sets[0][1:], drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.4)
plt.xlabel(x_label,fontsize=28)
plt.ylabel('Number of synthetic objects',fontsize=28)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
ax3 = ax1.twiny()
ax3.set_xlabel('Maximum Solar Elongation (degrees)',fontsize=26)
#max_solar_elong_deg = np.round(np.degrees(np.arcsin(bins/1.)),1)
max_solar_elong_deg = np.arange(20.5,55.5,3.5)
bins2=  np.arange(.35,.825,.05)
bins2[1:] +=0.025
plt.plot(max_solar_elong_deg,bins2, drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.0)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
#ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
#ax2.set_ylabel('Completeness',fontsize=28)  # we already handled the x-label with ax1
#plt.step(np.append(np.append(bins[np.where(data_sets[1]>0.0)][0], bins[np.where(data_sets[1]>0.0)]),bins[np.where(data_sets[1]>0.0)][-1]), np.append(0.0,np.append(data_sets[1][np.where(data_sets[1]>0.0)],0.0)),linewidth=2.5*1.25,label = r'$\mathrm{' + notes[1] +'}$',color='red')
#plt.step(bins[1:14], data_sets[1][1:14],linewidth=2.5*1.25,label = r'$\mathrm{' + notes[1] +'}$',color='red')
#error_bar_high = data_sets[3] - data_sets[1]
#error_bar_low = data_sets[1] - data_sets[2]
#plt.errorbar(bins[1:][np.where(data_sets[1][1:]>0.0)]-(np.diff(bins)[0]/2.), data_sets[1][1:][np.where(data_sets[1][1:]>0.0)], yerr=[error_bar_low[1:][np.where(data_sets[1][1:]>0.0)],error_bar_high[1:][np.where(data_sets[1][1:]>0.0)]],color='red',linewidth=2.5*1.25,fmt='o',capsize=5*1.25, elinewidth=2*1.25,markeredgewidth=2*1.25,ms=0.1)
#plt.axvline(vline_loc, color='black',linestyle='--',linewidth=5)
#ax1.set_yticks(ytick_locs)
ax1.set_xticks(xtick_locs[::2])
ax1.set_xlim(xlim_min,xlim_max)
ax3.set_xticks(max_solar_elong_deg[::2]+1)
ax3.set_xticklabels(max_solar_elong_deg[::2].astype('string'))
MyFormatter2 = FuncFormatter(latex_ticks1)
ax3.axes.xaxis.set_major_formatter(MyFormatter2)
ax3.set_xlim(max_solar_elong_deg.min(),max_solar_elong_deg.max())
ax1.set_ylim(0.0,(data_sets[0].max()*1.0)*1.25)
#ax2.set_ylim(0.0,(data_sets[3].max()*1.0)*1.25)
ax2.set_ylim(0.0,0.6)
plt.tick_params(labelsize=28)
fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
plt.savefig(plt_dir + file_name + ext)


#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.0
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H magnitude'
file_name='synth_H_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



a_hist_bins_min = 15.5
a_hist_bins_max = 21.75
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = 18.29 #assumes 0.275 colors for S type
xlim_min,xlim_max = 15.5,21.5

x_label = 'V magnitude'
file_name='synth_V_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(found_V,bins)
second_hist_values = make_weighted_histogram(found_V,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#rate of motion
a_hist_bins_min = 0.0
a_hist_bins_max = 2.7
delta_a_hist_bins = 0.1
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = ((np.sqrt(95.26720**2 + 86.59457**2))*24)/3600. #from horizons, in deg per day
xlim_min,xlim_max = 0.0, 2.7

x_label = 'Rate of motion (deg/day)'
file_name='synth_rom_detected'
xtick_locs = a_hist_bins

rom_synth = np.sqrt(found_AstRARate_deg_day**2 + found_AstDecRate_deg_day**2)
#set to our a datasets
first_hist_values = make_histogram1d(rom_synth,bins)
second_hist_values = make_weighted_histogram(rom_synth,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[i] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[i]]).T

#official elements
a_off = 0.55536
e_off = 0.177296
i_off = 15.880
H_off = 16.4
first_note = 'Synthetic population'
second_note = 'Synthetic population detected'
notes = np.array([first_note,second_note])
plt_dir='./'
ext = '.png'
#ytick_locs = np.arange(0,1.2,0.1)



#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.0
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H magnitude'
file_name='synth_H_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#V_mag distribution

a_hist_bins_min = 15.5
a_hist_bins_max = 21.75
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = 18.29 #assumes 0.275 colors for S type
xlim_min,xlim_max = 15.5,21.5

x_label = 'V magnitude'
file_name='synth_V_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(found_V,bins)
second_hist_values = make_weighted_histogram(found_V,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#rate of motion
a_hist_bins_min = 0.0
a_hist_bins_max = 2.7
delta_a_hist_bins = 0.1
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = ((np.sqrt(95.26720**2 + 86.59457**2))*24)/3600. #from horizons, in deg per day
xlim_min,xlim_max = 0.0, 2.7

x_label = 'Rate of motion (deg/day)'
file_name='synth_rom_detected'
xtick_locs = a_hist_bins

rom_synth = np.sqrt(found_AstRARate_deg_day**2 + found_AstDecRate_deg_day**2)
#set to our a datasets
first_hist_values = make_histogram1d(rom_synth,bins)
second_hist_values = make_weighted_histogram(rom_synth,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#completeness histograms WORKS works

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[i] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[i]]).T

#official elements
a_off = 0.55536
e_off = 0.177296
i_off = 15.880
H_off = 16.4
first_note = 'Synthetic population'
second_note = 'Synthetic population detected'
notes = np.array([first_note,second_note])
plt_dir='./'
ext = '.png'
#ytick_locs = np.arange(0,1.2,0.1)


#a hist
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'a (au)'
file_name='synth_a_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins)) + make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],frank_a_e_i_H_efficiency[:,4][np.where(frank_a_e_i_H_efficiency[:,0]<0.65)],a_hist_bins)/((-1.0 *np.sqrt(make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))) + make_histogram1d(vat_all_a_au[np.where(vat_all_a_au<0.65)],bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties_alternative(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#e hist
a_hist_bins_min = 0.0
a_hist_bins_max = 1.05
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = e_off
xlim_min,xlim_max = 0.0,1.0

x_label = 'e'
file_name='synth_e_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_e,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_e,bins)) + make_histogram1d(vat_all_e,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,1],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_e,bins)) + make_histogram1d(vat_all_e,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#i hist
a_hist_bins_min = 0.0
a_hist_bins_max = 85
delta_a_hist_bins = 5
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = i_off
xlim_min,xlim_max = 0.0,85

x_label = 'i (deg)'
file_name='synth_i_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_i_deg,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_i_deg,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_i_deg,bins)) + make_histogram1d(vat_all_i_deg,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,2],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_i_deg,bins)) + make_histogram1d(vat_all_i_deg,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#Q hist
a_hist_bins_min = 0.35
a_hist_bins_max = 0.825
delta_a_hist_bins = 0.025
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = a_off * (1+e_off)
xlim_min,xlim_max = a_hist_bins.min(),a_hist_bins.max()

x_label = 'Q (au)'
file_name='synth_Q_detected_with_errors_v2'
xtick_locs = a_hist_bins

vat_all_Q_au = (1.+vat_all_e)*vat_all_a_au

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/first_hist_values
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins)) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,0]*(1.+frank_a_e_i_H_efficiency[:,1]),frank_a_e_i_H_efficiency[:,4],a_hist_bins)/((-1.0 *np.sqrt(make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))) + make_histogram1d(vat_all_a_au*(1.+vat_all_e),bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])


xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max = xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max

fig = plt.figure(figsize=(12 - 2*margin, 10 - 2*margin))
ax1 = fig.add_subplot(111)
plt.fill_between(bins,data_sets[0], step="pre", alpha=0.4,color='grey')
plt.plot(bins[1:],data_sets[0][1:], drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.4)
plt.xlabel(x_label,fontsize=28)
plt.ylabel('Number of synthetic objects',fontsize=28)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
ax3 = ax1.twiny()
ax3.set_xlabel('Maximum Solar Elongation (degrees)',fontsize=26)
#max_solar_elong_deg = np.round(np.degrees(np.arcsin(bins/1.)),1)
max_solar_elong_deg = np.arange(20.5,55.5,3.5)
bins2=  np.arange(.35,.825,.05)
bins2[1:] +=0.025
plt.plot(max_solar_elong_deg,bins2, drawstyle="steps",color='black',linewidth=2.5*1.25,alpha=0.0)
plt.tick_params(labelsize=28)
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
ax2.set_ylabel('Completeness',fontsize=28)  # we already handled the x-label with ax1
plt.step(bins[1:14], data_sets[1][1:14],linewidth=2.5*1.25,label = r'$\mathrm{' + notes[1] +'}$',color='red')
error_bar_high = data_sets[3] - data_sets[1]
error_bar_low = data_sets[1] - data_sets[2]
plt.errorbar(bins[1:][np.where(data_sets[1][1:]>0.0)]-(np.diff(bins)[0]/2.), data_sets[1][1:][np.where(data_sets[1][1:]>0.0)], yerr=[error_bar_low[1:][np.where(data_sets[1][1:]>0.0)],error_bar_high[1:][np.where(data_sets[1][1:]>0.0)]],color='red',linewidth=2.5*1.25,fmt='o',capsize=5*1.25, elinewidth=2*1.25,markeredgewidth=2*1.25,ms=0.1)
plt.axvline(vline_loc, color='black',linestyle='--',linewidth=5)
ax1.set_xticks(xtick_locs[::2])
ax1.set_xlim(xlim_min,xlim_max)
ax3.set_xticks(max_solar_elong_deg[::2]+1)
ax3.set_xticklabels(max_solar_elong_deg[::2].astype('string'))
MyFormatter2 = FuncFormatter(latex_ticks1)
ax3.axes.xaxis.set_major_formatter(MyFormatter2)
ax3.set_xlim(max_solar_elong_deg.min(),max_solar_elong_deg.max())
ax1.set_ylim(0.0,(data_sets[0].max()*1.0)*1.25)
#ax2.set_ylim(0.0,(data_sets[3].max()*1.0)*1.25)
ax2.set_ylim(0.0,0.6)
plt.tick_params(labelsize=28)
fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.xticks(fontsize=28)
plt.yticks(fontsize=28)
plt.savefig(plt_dir + file_name + ext)

#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.0
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H magnitude'
file_name='synth_H_detected_with_errors_v2'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#H hist higher rez
a_hist_bins_min = 15.0
a_hist_bins_max = 19.0
delta_a_hist_bins = 0.1
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,18.5

x_label = 'H magnitude'
file_name='synth_H_detected_with_unc_0_1_bin_size'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(np.array(random.sample(vat_all_H,2445)),bins)
second_hist_values = np.zeros(len(first_hist_values))+0.00001
third_hist_values = np.zeros(len(first_hist_values))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = np.zeros(len(first_hist_values))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties_v2(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#granvik et al. 0.25 bins


#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.0
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
a_hist_bins += 0.125
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H magnitude'
file_name='synth_H_detected_with_errors_v2_Granvik_2018_bins'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])
make_hist_two_datasets_no_legend_with_uncertainties(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


for i in range(0,len(bins)):
    print bins[i], first_hist_values[i]

a_hist_bins_min = 15.5
a_hist_bins_max = 21.75
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = 18.29 #assumes 0.275 colors for S type
xlim_min,xlim_max = 15.5,21.5

x_label = 'V magnitude'
file_name='synth_V_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(found_V,bins)
second_hist_values = make_weighted_histogram(found_V,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#rate of motion
a_hist_bins_min = 0.0
a_hist_bins_max = 2.7
delta_a_hist_bins = 0.1
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = ((np.sqrt(95.26720**2 + 86.59457**2))*24)/3600. #from horizons, in deg per day
xlim_min,xlim_max = 0.0, 2.7

x_label = 'Rate of motion (deg/day)'
file_name='synth_rom_detected'
xtick_locs = a_hist_bins

rom_synth = np.sqrt(found_AstRARate_deg_day**2 + found_AstDecRate_deg_day**2)
#set to our a datasets
first_hist_values = make_histogram1d(rom_synth,bins)
second_hist_values = make_weighted_histogram(rom_synth,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#rate of motion arcsec per hour
a_hist_bins_min = 0.0*(3600./24.)
a_hist_bins_max = 2.7*(3600./24.)
delta_a_hist_bins = 0.1*(3600./24.)
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = (((np.sqrt(95.26720**2 + 86.59457**2))*24)/3600.)*(3600./24.) #from horizons, in deg per day
xlim_min,xlim_max = 0.0*(3600./24.), 2.7*(3600./24.)

x_label = 'Rate of motion (arcseconds/hour)'
file_name='synth_rom_detected'
xtick_locs = a_hist_bins

rom_synth = np.sqrt(found_AstRARate_deg_day**2 + found_AstDecRate_deg_day**2) *(3600./24.)
#set to our a datasets
first_hist_values = make_histogram1d(rom_synth,bins)
second_hist_values = make_weighted_histogram(rom_synth,detection_efficiency(found_V,0.87, 20.6,0.73),a_hist_bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


'''
Morbidelli albedo model

here you go:

      name                    H,       a,                e,                      i,                Pnu6,             P5:2,           P2:1,              Phu,            P3:1,           Ppho,           Pjfc
     Vatira               16.40   0.555452  0.1770081   15.87023   0.77956003  0.0000000  0.0000000   0.1796000  0.0397200  0.0011300  0.0000000


Pnu6,             P5:2,           P2:1,              Phu,            P3:1,           Ppho,           Pjfc
0.77956003        0.0000000       0.0000000          0.1796000       0.0397200       0.0011300       0.0000000

Morby

nu6

nu6 
p1 0.120 +/- 0.014
p2 0.558 +/- 0.003
p3 0.322 +/- 0.014

31
p1 0.144 +/- 0.034
p2 0.782 +/- 0.036
p3 0.074 +/- 0.050

phoc
p1 0.501 +/- 0.010
p2 0.452 +/- 0.020
p3 0.047 +/- 0.022



'''
#albedo of 2020 AV2
#from Morbidelli et al. 2020

#albedo of three different categories
p_1 = 0.05
p_2 = 0.2
p_3 = 0.3

#nu6
p1_nu6,p1_nu6_unc = 0.120,0.014
p2_nu6,p2_nu6_unc = 0.558,0.003
p3_nu6,p3_nu6_unc = 0.322,0.014

#31
p1_31,p1_31_unc = 0.144,0.034
p2_31,p2_31_unc = 0.782,0.036
p3_31,p3_31_unc = 0.074,0.050

#hun,
p1_hun,p1_hun_unc = 0.021,0.005
p2_hun,p2_hun_unc = 0.113,0.004
p3_hun,p3_hun_unc = 0.866,0.006

p1_phoc,p1_phoc_unc = 0.501,0.01
p2_phoc,p2_phoc_unc = 0.452,0.02
p3_phoc,p3_phoc_unc = 0.047,0.022

p1_52,p1_52_unc = 0.294,0.047
p2_52,p2_52_unc = 0.557,0.039
p3_52,p3_52_unc = 0.149,0.061

p1_21,p1_21_unc = 0.399, 0.015
p2_21,p2_21_unc = 0.200, 0.036
p3_21,p3_21_unc = 0.461, 0.039

#for 2020_AV2
s_nu6 = 0.77956003
s_31 = 0.0397200
s_hun = 0.1796000
s_phoc = 0.0
s_52 = 0.0
s_21 = 0.0

av_albedo_nu6 = (((p1_nu6 * p_1 ) + (p2_nu6 * p_2 ) + (p3_nu6 * p_3 ))) * s_nu6
av_albedo_31 = (((p1_31 * p_1) + (p2_31 * p_2) + (p3_31 * p_3))) * s_31
av_albedo_hun = ((p1_hun * p_1) + (p2_hun * p_2) + (p3_hun * p_3))  * s_hun
av_albedo_phoc = ((p1_phoc * p_1) + (p2_phoc * p_2) + (p3_phoc * p_3))  * s_phoc
av_albedo_52 = ((p1_52 * p_1) + (p2_52 * p_2) + (p3_52 * p_3))  * s_52
av_albedo_21 = ((p1_21 * p_1) + (p2_21 * p_2) + (p3_21 * p_3))  * s_21

av_albedo_nu6_unc = np.sqrt(((p1_nu6_unc * p_1 )**2 + (p2_nu6_unc * p_2 )**2 + (p3_nu6_unc * p_3 )**2)) * s_nu6
av_albedo_31_unc = np.sqrt(((p1_31_unc * p_1)**2 + (p2_31_unc * p_2)**2 + (p3_31_unc * p_3)**2)) * s_31
av_albedo_hun_unc = np.sqrt((p1_hun_unc * p_1)**2 + (p2_hun_unc * p_2)**2 + (p3_hun_unc * p_3)**2)  * s_hun
av_albedo_phoc_unc = np.sqrt((p1_phoc_unc * p_1)**2 + (p2_phoc_unc * p_2)**2 + (p3_phoc_unc * p_3)**2)  * s_phoc
av_albedo_52_unc = np.sqrt((p1_52_unc * p_1)**2 + (p2_52_unc * p_2)**2 + (p3_52_unc * p_3)**2)  * s_52
av_albedo_21_unc = np.sqrt((p1_21_unc * p_1)**2 + (p2_21_unc * p_2)**2 + (p3_21_unc * p_3)**2)  * s_21


av_albedo_all = av_albedo_nu6 + av_albedo_31 + av_albedo_hun + av_albedo_phoc + av_albedo_52 + av_albedo_21
av_albedo_all_unc = np.sqrt(av_albedo_nu6_unc**2 + av_albedo_31_unc**2 + av_albedo_hun_unc**2 + av_albedo_phoc_unc**2 + av_albedo_52_unc**2 + av_albedo_21_unc**2)

print morbidelli_albedo_from_source(s_nu6,s_31,s_hun,s_phoc,s_52,s_21)

'''
print av_albedo_all
0.22526935442600005
'''

'''
additional tests from morby
id     H,       a,                      e, i,Pnu6,               P5:2, P2:1,             Phu,     P3:1,            Ppho, Pjfc
1 16.40   0.555446  0.1769655   15.86808   0.77956003 0.0000000  0.0000000  0.1796000  0.0397200  0.0011300  0.0000000
2 16.65   0.555446  0.1769655   15.86808   0.77956003 0.0000000  0.0000000  0.1796000  0.0397200  0.0011300  0.0000000
3 16.15   0.555446  0.1769655   15.86808   0.77956003 0.0000000  0.0000000  0.1796000  0.0397200  0.0011300  0.0000000
4 16.40   0.555446  0.1769655   11.86808   0.75546002 0.0000000  0.0000000  0.1543900  0.0854900  0.0046500  0.0000000
5 16.65   0.555446  0.1769655   11.86808   0.75546002 0.0000000  0.0000000  0.1543900  0.0854900  0.0046500  0.0000000
6 16.15   0.555446  0.1769655   11.86808   0.75546002 0.0000000  0.0000000  0.1543900  0.0854900  0.0046500  0.0000000
7 16.40   0.555446  0.1769655   19.86808   0.83257997 0.0000000  0.0000000  0.1283100  0.0391200  0.0000000  0.0000000
8 16.65   0.555446  0.1769655   19.86808   0.83257997 0.0000000  0.0000000  0.1283100  0.0391200  0.0000000  0.0000000
9 16.15   0.555446  0.1769655   19.86808   0.83257997 0.0000000  0.0000000  0.1283100  0.0391200  0.0000000  0.0000000
10 16.40   0.555446  0.1369655   15.86808   0.79790998 0.0000000  0.0000000  0.1494400  0.0508300  0.0018200  0.0000000
11 16.65   0.555446  0.1369655   15.86808   0.79790998 0.0000000  0.0000000  0.1494400  0.0508300  0.0018200  0.0000000
12 16.15   0.555446  0.1369655   15.86808   0.79790998 0.0000000  0.0000000  0.1494400  0.0508300  0.0018200  0.0000000
13 16.40   0.555446  0.1369655   11.86808   0.77266997 0.0000000  0.0000000  0.1463400  0.0760800  0.0049100  0.0000000
14 16.65   0.555446  0.1369655   11.86808   0.77266997 0.0000000  0.0000000  0.1463400  0.0760800  0.0049100  0.0000000
15 16.15   0.555446  0.1369655   11.86808   0.77266997 0.0000000  0.0000000  0.1463400  0.0760800  0.0049100  0.0000000
16 16.40   0.555446  0.1369655   19.86808   0.82730001 0.0000000  0.0000000  0.1486800  0.0239700  0.0000500  0.0000000
17 16.65   0.555446  0.1369655   19.86808   0.82730001 0.0000000  0.0000000  0.1486800  0.0239700  0.0000500  0.0000000
18 16.15   0.555446  0.1369655   19.86808   0.82730001 0.0000000  0.0000000  0.1486800  0.0239700  0.0000500  0.0000000
19 16.40   0.555446  0.2169655   15.86808   0.76485997 0.0000000  0.0000000  0.1865300  0.0484200  0.0001900  0.0000000
20 16.65   0.555446  0.2169655   15.86808   0.76485997 0.0000000  0.0000000  0.1865300  0.0484200  0.0001900  0.0000000
21 16.15   0.555446  0.2169655   15.86808   0.76485997 0.0000000  0.0000000  0.1865300  0.0484200  0.0001900  0.0000000
22 16.40   0.555446  0.2169655   11.86808   0.78422999 0.0000000  0.0000000  0.1666400  0.0467600  0.0023600  0.0000000
23 16.65   0.555446  0.2169655   11.86808   0.78422999 0.0000000  0.0000000  0.1666400  0.0467600  0.0023600  0.0000000
24 16.15   0.555446  0.2169655   11.86808   0.78422999 0.0000000  0.0000000  0.1666400  0.0467600  0.0023600  0.0000000
25 16.40   0.555446  0.2169655   19.86808   0.81708002 0.0000000  0.0029900  0.1262000  0.0536200  0.0001200  0.0000000
26 16.65   0.555446  0.2169655   19.86808   0.81708002 0.0000000  0.0029900  0.1262000  0.0536200  0.0001200  0.0000000
27 16.15   0.555446  0.2169655   19.86808   0.81708002 0.0000000  0.0029900  0.1262000  0.0536200  0.0001200  0.0000000
28 16.40   0.455446  0.1769655   15.86808   0.45596999 0.0000000  0.0000000  0.1702800  0.3737500  0.0000000  0.0000000
29 16.65   0.455446  0.1769655   15.86808   0.45596999 0.0000000  0.0000000  0.1702800  0.3737500  0.0000000  0.0000000
30 16.15   0.455446  0.1769655   15.86808   0.45596999 0.0000000  0.0000000  0.1702800  0.3737500  0.0000000  0.0000000
31 16.40   0.455446  0.1769655   11.86808   0.43599999 0.0000000  0.0000000  0.0829300  0.4810700  0.0000000  0.0000000
32 16.65   0.455446  0.1769655   11.86808   0.43599999 0.0000000  0.0000000  0.0829300  0.4810700  0.0000000  0.0000000
33 16.15   0.455446  0.1769655   11.86808   0.43599999 0.0000000  0.0000000  0.0829300  0.4810700  0.0000000  0.0000000
34 16.40   0.455446  0.1769655   19.86808   0.15662000 0.0000000  0.0000000  0.0510000  0.7923800  0.0000000  0.0000000
35 16.65   0.455446  0.1769655   19.86808   0.15662000 0.0000000  0.0000000  0.0510000  0.7923800  0.0000000  0.0000000
36 16.15   0.455446  0.1769655   19.86808   0.15662000 0.0000000  0.0000000  0.0510000  0.7923800  0.0000000  0.0000000
37 16.40   0.455446  0.1369655   15.86808   0.55994999 0.0000000  0.0000000  0.1744200  0.2656300  0.0000000  0.0000000
38 16.65   0.455446  0.1369655   15.86808   0.55994999 0.0000000  0.0000000  0.1744200  0.2656300  0.0000000  0.0000000
39 16.15   0.455446  0.1369655   15.86808   0.55994999 0.0000000  0.0000000  0.1744200  0.2656300  0.0000000  0.0000000
40 16.40   0.455446  0.1369655   11.86808   0.49349999 0.0000000  0.0000000  0.1096100  0.3968900  0.0000000  0.0000000
41 16.65   0.455446  0.1369655   11.86808   0.49349999 0.0000000  0.0000000  0.1096100  0.3968900  0.0000000  0.0000000
42 16.15   0.455446  0.1369655   11.86808   0.49349999 0.0000000  0.0000000  0.1096100  0.3968900  0.0000000  0.0000000
43 16.40   0.455446  0.1369655   19.86808   0.15172000 0.0000000  0.0000000  0.0573800  0.7909000  0.0000000  0.0000000
44 16.65   0.455446  0.1369655   19.86808   0.15172000 0.0000000  0.0000000  0.0573800  0.7909000  0.0000000  0.0000000
45 16.15   0.455446  0.1369655   19.86808   0.15172000 0.0000000  0.0000000  0.0573800  0.7909000  0.0000000  0.0000000
46 16.40   0.455446  0.2169655   15.86808   0.45861000 0.0000000  0.0000000  0.1135100  0.4278800  0.0000000  0.0000000
47 16.65   0.455446  0.2169655   15.86808   0.45861000 0.0000000  0.0000000  0.1135100  0.4278800  0.0000000  0.0000000
48 16.15   0.455446  0.2169655   15.86808   0.45861000 0.0000000  0.0000000  0.1135100  0.4278800  0.0000000  0.0000000
49 16.40   0.455446  0.2169655   11.86808   0.57674003 0.0000000  0.0000000  0.0947900  0.3284700  0.0000000  0.0000000
50 16.65   0.455446  0.2169655   11.86808   0.57674003 0.0000000  0.0000000  0.0947900  0.3284700  0.0000000  0.0000000
51 16.15   0.455446  0.2169655   11.86808   0.57674003 0.0000000  0.0000000  0.0947900  0.3284700  0.0000000  0.0000000
52 16.40   0.455446  0.2169655   19.86808   0.23300000 0.0000000  0.0000000  0.0474200  0.7195800  0.0000000  0.0000000
53 16.65   0.455446  0.2169655   19.86808   0.23300000 0.0000000  0.0000000  0.0474200  0.7195800  0.0000000  0.0000000
54 16.15   0.455446  0.2169655   19.86808   0.23300000 0.0000000  0.0000000  0.0474200  0.7195800  0.0000000  0.0000000
55 16.40   0.655446  0.1769655   15.86808   0.74258000 0.0000000  0.0753300  0.1608100  0.0177500  0.0035200  0.0000000
56 16.65   0.655446  0.1769655   15.86808   0.74258000 0.0000000  0.0753300  0.1608100  0.0177500  0.0035200  0.0000000
57 16.15   0.655446  0.1769655   15.86808   0.74258000 0.0000000  0.0753300  0.1608100  0.0177500  0.0035200  0.0000000
58 16.40   0.655446  0.1769655   11.86808   0.76099002 0.0000000  0.0066500  0.2078500  0.0207100  0.0037900  0.0000000
59 16.65   0.655446  0.1769655   11.86808   0.76099002 0.0000000  0.0066500  0.2078500  0.0207100  0.0037900  0.0000000
60 16.15   0.655446  0.1769655   11.86808   0.76099002 0.0000000  0.0066500  0.2078500  0.0207100  0.0037900  0.0000000
61 16.40   0.655446  0.1769655   19.86808   0.63910002 0.0000000  0.1580700  0.1466200  0.0504800  0.0057300  0.0000000
62 16.65   0.655446  0.1769655   19.86808   0.63910002 0.0000000  0.1580700  0.1466200  0.0504800  0.0057300  0.0000000
63 16.15   0.655446  0.1769655   19.86808   0.63910002 0.0000000  0.1580700  0.1466200  0.0504800  0.0057300  0.0000000
64 16.40   0.655446  0.1369655   15.86808   0.66075999 0.0000000  0.1031000  0.1747000  0.0580100  0.0034400  0.0000000
65 16.65   0.655446  0.1369655   15.86808   0.66075999 0.0000000  0.1031000  0.1747000  0.0580100  0.0034400  0.0000000
66 16.15   0.655446  0.1369655   15.86808   0.66075999 0.0000000  0.1031000  0.1747000  0.0580100  0.0034400  0.0000000
67 16.40   0.655446  0.1369655   11.86808   0.73558998 0.0000000  0.0028400  0.2528700  0.0066000  0.0021100  0.0000000
68 16.65   0.655446  0.1369655   11.86808   0.73558998 0.0000000  0.0028400  0.2528700  0.0066000  0.0021100  0.0000000
69 16.15   0.655446  0.1369655   11.86808   0.73558998 0.0000000  0.0028400  0.2528700  0.0066000  0.0021100  0.0000000
70 16.40   0.655446  0.1369655   19.86808   0.58538997 0.0000000  0.2155300  0.1465000  0.0485900  0.0039900  0.0000000
71 16.65   0.655446  0.1369655   19.86808   0.58538997 0.0000000  0.2155300  0.1465000  0.0485900  0.0039900  0.0000000
72 16.15   0.655446  0.1369655   19.86808   0.58538997 0.0000000  0.2155300  0.1465000  0.0485900  0.0039900  0.0000000
73 16.40   0.655446  0.2169655   15.86808   0.69310999 0.0000000  0.1423100  0.1402500  0.0215600  0.0027800  0.0000000
74 16.65   0.655446  0.2169655   15.86808   0.69310999 0.0000000  0.1423100  0.1402500  0.0215600  0.0027800  0.0000000
75 16.15   0.655446  0.2169655   15.86808   0.69310999 0.0000000  0.1423100  0.1402500  0.0215600  0.0027800  0.0000000
76 16.40   0.655446  0.2169655   11.86808   0.78178000 0.0000000  0.0356300  0.1590400  0.0203100  0.0032400  0.0000000
77 16.65   0.655446  0.2169655   11.86808   0.78178000 0.0000000  0.0356300  0.1590400  0.0203100  0.0032400  0.0000000
78 16.15   0.655446  0.2169655   11.86808   0.78178000 0.0000000  0.0356300  0.1590400  0.0203100  0.0032400  0.0000000
79 16.40   0.655446  0.2169655   19.86808   0.60479999 0.0000000  0.2198200  0.1243000  0.0456700  0.0054000  0.0000000
80 16.65   0.655446  0.2169655   19.86808   0.60479999 0.0000000  0.2198200  0.1243000  0.0456700  0.0054000  0.0000000
81 16.15   0.655446  0.2169655   19.86808   0.60479999 0.0000000  0.2198200  0.1243000  0.0456700  0.0054000  0.0000000

pbpaste | awk '{print $'1',$'6', $'7', $'8', $'9', $'10', $'11', $'12'}' > test

 cat test | awk '{print "print " "\"",$'1',"\"",",morbidelli_albedo_from_source(",$'2',",",$'3',",",$'4',",",$'5',",",$'6',",",$'7',")"}'

'''


print " 1 " ,morbidelli_albedo_from_source( 0.77956003 , 0.0000000 , 0.0000000 , 0.1796000 , 0.0397200 , 0.0011300 )
print " 2 " ,morbidelli_albedo_from_source( 0.77956003 , 0.0000000 , 0.0000000 , 0.1796000 , 0.0397200 , 0.0011300 )
print " 3 " ,morbidelli_albedo_from_source( 0.77956003 , 0.0000000 , 0.0000000 , 0.1796000 , 0.0397200 , 0.0011300 )
print " 4 " ,morbidelli_albedo_from_source( 0.75546002 , 0.0000000 , 0.0000000 , 0.1543900 , 0.0854900 , 0.0046500 )
print " 5 " ,morbidelli_albedo_from_source( 0.75546002 , 0.0000000 , 0.0000000 , 0.1543900 , 0.0854900 , 0.0046500 )
print " 6 " ,morbidelli_albedo_from_source( 0.75546002 , 0.0000000 , 0.0000000 , 0.1543900 , 0.0854900 , 0.0046500 )
print " 7 " ,morbidelli_albedo_from_source( 0.83257997 , 0.0000000 , 0.0000000 , 0.1283100 , 0.0391200 , 0.0000000 )
print " 8 " ,morbidelli_albedo_from_source( 0.83257997 , 0.0000000 , 0.0000000 , 0.1283100 , 0.0391200 , 0.0000000 )
print " 9 " ,morbidelli_albedo_from_source( 0.83257997 , 0.0000000 , 0.0000000 , 0.1283100 , 0.0391200 , 0.0000000 )
print " 10 " ,morbidelli_albedo_from_source( 0.79790998 , 0.0000000 , 0.0000000 , 0.1494400 , 0.0508300 , 0.0018200 )
print " 11 " ,morbidelli_albedo_from_source( 0.79790998 , 0.0000000 , 0.0000000 , 0.1494400 , 0.0508300 , 0.0018200 )
print " 12 " ,morbidelli_albedo_from_source( 0.79790998 , 0.0000000 , 0.0000000 , 0.1494400 , 0.0508300 , 0.0018200 )
print " 13 " ,morbidelli_albedo_from_source( 0.77266997 , 0.0000000 , 0.0000000 , 0.1463400 , 0.0760800 , 0.0049100 )
print " 14 " ,morbidelli_albedo_from_source( 0.77266997 , 0.0000000 , 0.0000000 , 0.1463400 , 0.0760800 , 0.0049100 )
print " 15 " ,morbidelli_albedo_from_source( 0.77266997 , 0.0000000 , 0.0000000 , 0.1463400 , 0.0760800 , 0.0049100 )
print " 16 " ,morbidelli_albedo_from_source( 0.82730001 , 0.0000000 , 0.0000000 , 0.1486800 , 0.0239700 , 0.0000500 )
print " 17 " ,morbidelli_albedo_from_source( 0.82730001 , 0.0000000 , 0.0000000 , 0.1486800 , 0.0239700 , 0.0000500 )
print " 18 " ,morbidelli_albedo_from_source( 0.82730001 , 0.0000000 , 0.0000000 , 0.1486800 , 0.0239700 , 0.0000500 )
print " 19 " ,morbidelli_albedo_from_source( 0.76485997 , 0.0000000 , 0.0000000 , 0.1865300 , 0.0484200 , 0.0001900 )
print " 20 " ,morbidelli_albedo_from_source( 0.76485997 , 0.0000000 , 0.0000000 , 0.1865300 , 0.0484200 , 0.0001900 )
print " 21 " ,morbidelli_albedo_from_source( 0.76485997 , 0.0000000 , 0.0000000 , 0.1865300 , 0.0484200 , 0.0001900 )
print " 22 " ,morbidelli_albedo_from_source( 0.78422999 , 0.0000000 , 0.0000000 , 0.1666400 , 0.0467600 , 0.0023600 )
print " 23 " ,morbidelli_albedo_from_source( 0.78422999 , 0.0000000 , 0.0000000 , 0.1666400 , 0.0467600 , 0.0023600 )
print " 24 " ,morbidelli_albedo_from_source( 0.78422999 , 0.0000000 , 0.0000000 , 0.1666400 , 0.0467600 , 0.0023600 )
print " 25 " ,morbidelli_albedo_from_source( 0.81708002 , 0.0000000 , 0.0029900 , 0.1262000 , 0.0536200 , 0.0001200 )
print " 26 " ,morbidelli_albedo_from_source( 0.81708002 , 0.0000000 , 0.0029900 , 0.1262000 , 0.0536200 , 0.0001200 )
print " 27 " ,morbidelli_albedo_from_source( 0.81708002 , 0.0000000 , 0.0029900 , 0.1262000 , 0.0536200 , 0.0001200 )
print " 28 " ,morbidelli_albedo_from_source( 0.45596999 , 0.0000000 , 0.0000000 , 0.1702800 , 0.3737500 , 0.0000000 )
print " 29 " ,morbidelli_albedo_from_source( 0.45596999 , 0.0000000 , 0.0000000 , 0.1702800 , 0.3737500 , 0.0000000 )
print " 30 " ,morbidelli_albedo_from_source( 0.45596999 , 0.0000000 , 0.0000000 , 0.1702800 , 0.3737500 , 0.0000000 )
print " 31 " ,morbidelli_albedo_from_source( 0.43599999 , 0.0000000 , 0.0000000 , 0.0829300 , 0.4810700 , 0.0000000 )
print " 32 " ,morbidelli_albedo_from_source( 0.43599999 , 0.0000000 , 0.0000000 , 0.0829300 , 0.4810700 , 0.0000000 )
print " 33 " ,morbidelli_albedo_from_source( 0.43599999 , 0.0000000 , 0.0000000 , 0.0829300 , 0.4810700 , 0.0000000 )
print " 34 " ,morbidelli_albedo_from_source( 0.15662000 , 0.0000000 , 0.0000000 , 0.0510000 , 0.7923800 , 0.0000000 )
print " 35 " ,morbidelli_albedo_from_source( 0.15662000 , 0.0000000 , 0.0000000 , 0.0510000 , 0.7923800 , 0.0000000 )
print " 36 " ,morbidelli_albedo_from_source( 0.15662000 , 0.0000000 , 0.0000000 , 0.0510000 , 0.7923800 , 0.0000000 )
print " 37 " ,morbidelli_albedo_from_source( 0.55994999 , 0.0000000 , 0.0000000 , 0.1744200 , 0.2656300 , 0.0000000 )
print " 38 " ,morbidelli_albedo_from_source( 0.55994999 , 0.0000000 , 0.0000000 , 0.1744200 , 0.2656300 , 0.0000000 )
print " 39 " ,morbidelli_albedo_from_source( 0.55994999 , 0.0000000 , 0.0000000 , 0.1744200 , 0.2656300 , 0.0000000 )
print " 40 " ,morbidelli_albedo_from_source( 0.49349999 , 0.0000000 , 0.0000000 , 0.1096100 , 0.3968900 , 0.0000000 )
print " 41 " ,morbidelli_albedo_from_source( 0.49349999 , 0.0000000 , 0.0000000 , 0.1096100 , 0.3968900 , 0.0000000 )
print " 42 " ,morbidelli_albedo_from_source( 0.49349999 , 0.0000000 , 0.0000000 , 0.1096100 , 0.3968900 , 0.0000000 )
print " 43 " ,morbidelli_albedo_from_source( 0.15172000 , 0.0000000 , 0.0000000 , 0.0573800 , 0.7909000 , 0.0000000 )
print " 44 " ,morbidelli_albedo_from_source( 0.15172000 , 0.0000000 , 0.0000000 , 0.0573800 , 0.7909000 , 0.0000000 )
print " 45 " ,morbidelli_albedo_from_source( 0.15172000 , 0.0000000 , 0.0000000 , 0.0573800 , 0.7909000 , 0.0000000 )
print " 46 " ,morbidelli_albedo_from_source( 0.45861000 , 0.0000000 , 0.0000000 , 0.1135100 , 0.4278800 , 0.0000000 )
print " 47 " ,morbidelli_albedo_from_source( 0.45861000 , 0.0000000 , 0.0000000 , 0.1135100 , 0.4278800 , 0.0000000 )
print " 48 " ,morbidelli_albedo_from_source( 0.45861000 , 0.0000000 , 0.0000000 , 0.1135100 , 0.4278800 , 0.0000000 )
print " 49 " ,morbidelli_albedo_from_source( 0.57674003 , 0.0000000 , 0.0000000 , 0.0947900 , 0.3284700 , 0.0000000 )
print " 50 " ,morbidelli_albedo_from_source( 0.57674003 , 0.0000000 , 0.0000000 , 0.0947900 , 0.3284700 , 0.0000000 )
print " 51 " ,morbidelli_albedo_from_source( 0.57674003 , 0.0000000 , 0.0000000 , 0.0947900 , 0.3284700 , 0.0000000 )
print " 52 " ,morbidelli_albedo_from_source( 0.23300000 , 0.0000000 , 0.0000000 , 0.0474200 , 0.7195800 , 0.0000000 )
print " 53 " ,morbidelli_albedo_from_source( 0.23300000 , 0.0000000 , 0.0000000 , 0.0474200 , 0.7195800 , 0.0000000 )
print " 54 " ,morbidelli_albedo_from_source( 0.23300000 , 0.0000000 , 0.0000000 , 0.0474200 , 0.7195800 , 0.0000000 )
print " 55 " ,morbidelli_albedo_from_source( 0.74258000 , 0.0000000 , 0.0753300 , 0.1608100 , 0.0177500 , 0.0035200 )
print " 56 " ,morbidelli_albedo_from_source( 0.74258000 , 0.0000000 , 0.0753300 , 0.1608100 , 0.0177500 , 0.0035200 )
print " 57 " ,morbidelli_albedo_from_source( 0.74258000 , 0.0000000 , 0.0753300 , 0.1608100 , 0.0177500 , 0.0035200 )
print " 58 " ,morbidelli_albedo_from_source( 0.76099002 , 0.0000000 , 0.0066500 , 0.2078500 , 0.0207100 , 0.0037900 )
print " 59 " ,morbidelli_albedo_from_source( 0.76099002 , 0.0000000 , 0.0066500 , 0.2078500 , 0.0207100 , 0.0037900 )
print " 60 " ,morbidelli_albedo_from_source( 0.76099002 , 0.0000000 , 0.0066500 , 0.2078500 , 0.0207100 , 0.0037900 )
print " 61 " ,morbidelli_albedo_from_source( 0.63910002 , 0.0000000 , 0.1580700 , 0.1466200 , 0.0504800 , 0.0057300 )
print " 62 " ,morbidelli_albedo_from_source( 0.63910002 , 0.0000000 , 0.1580700 , 0.1466200 , 0.0504800 , 0.0057300 )
print " 63 " ,morbidelli_albedo_from_source( 0.63910002 , 0.0000000 , 0.1580700 , 0.1466200 , 0.0504800 , 0.0057300 )
print " 64 " ,morbidelli_albedo_from_source( 0.66075999 , 0.0000000 , 0.1031000 , 0.1747000 , 0.0580100 , 0.0034400 )
print " 65 " ,morbidelli_albedo_from_source( 0.66075999 , 0.0000000 , 0.1031000 , 0.1747000 , 0.0580100 , 0.0034400 )
print " 66 " ,morbidelli_albedo_from_source( 0.66075999 , 0.0000000 , 0.1031000 , 0.1747000 , 0.0580100 , 0.0034400 )
print " 67 " ,morbidelli_albedo_from_source( 0.73558998 , 0.0000000 , 0.0028400 , 0.2528700 , 0.0066000 , 0.0021100 )
print " 68 " ,morbidelli_albedo_from_source( 0.73558998 , 0.0000000 , 0.0028400 , 0.2528700 , 0.0066000 , 0.0021100 )
print " 69 " ,morbidelli_albedo_from_source( 0.73558998 , 0.0000000 , 0.0028400 , 0.2528700 , 0.0066000 , 0.0021100 )
print " 70 " ,morbidelli_albedo_from_source( 0.58538997 , 0.0000000 , 0.2155300 , 0.1465000 , 0.0485900 , 0.0039900 )
print " 71 " ,morbidelli_albedo_from_source( 0.58538997 , 0.0000000 , 0.2155300 , 0.1465000 , 0.0485900 , 0.0039900 )
print " 72 " ,morbidelli_albedo_from_source( 0.58538997 , 0.0000000 , 0.2155300 , 0.1465000 , 0.0485900 , 0.0039900 )
print " 73 " ,morbidelli_albedo_from_source( 0.69310999 , 0.0000000 , 0.1423100 , 0.1402500 , 0.0215600 , 0.0027800 )
print " 74 " ,morbidelli_albedo_from_source( 0.69310999 , 0.0000000 , 0.1423100 , 0.1402500 , 0.0215600 , 0.0027800 )
print " 75 " ,morbidelli_albedo_from_source( 0.69310999 , 0.0000000 , 0.1423100 , 0.1402500 , 0.0215600 , 0.0027800 )
print " 76 " ,morbidelli_albedo_from_source( 0.78178000 , 0.0000000 , 0.0356300 , 0.1590400 , 0.0203100 , 0.0032400 )
print " 77 " ,morbidelli_albedo_from_source( 0.78178000 , 0.0000000 , 0.0356300 , 0.1590400 , 0.0203100 , 0.0032400 )
print " 78 " ,morbidelli_albedo_from_source( 0.78178000 , 0.0000000 , 0.0356300 , 0.1590400 , 0.0203100 , 0.0032400 )
print " 79 " ,morbidelli_albedo_from_source( 0.60479999 , 0.0000000 , 0.2198200 , 0.1243000 , 0.0456700 , 0.0054000 )
print " 80 " ,morbidelli_albedo_from_source( 0.60479999 , 0.0000000 , 0.2198200 , 0.1243000 , 0.0456700 , 0.0054000 )
print " 81 " ,morbidelli_albedo_from_source( 0.60479999 , 0.0000000 , 0.2198200 , 0.1243000 , 0.0456700 , 0.0054000 )

'''
 1  (0.19725713692600003, 0.003714721920668777)
 2  (0.19725713692600003, 0.003714721920668777)
 3  (0.19725713692600003, 0.003714721920668777)
 4  (0.19734431528400004, 0.0038619839814633774)
 5  (0.19734431528400004, 0.0038619839814633774)
 6  (0.19734431528400004, 0.0038619839814633774)
 7  (0.201642886074, 0.0037968168231360283)
 8  (0.201642886074, 0.0037968168231360283)
 9  (0.201642886074, 0.0037968168231360283)
 10  (0.19931484871600003, 0.003760968483685422)
 11  (0.19931484871600003, 0.003760968483685422)
 12  (0.19931484871600003, 0.003760968483685422)
 13  (0.19843212607400001, 0.0038271155809476366)
 14  (0.19843212607400001, 0.0038271155809476366)
 15  (0.19843212607400001, 0.0038271155809476366)
 16  (0.200573144642, 0.003769311985195447)
 17  (0.200573144642, 0.003769311985195447)
 18  (0.200573144642, 0.003769311985195447)
 19  (0.19630577057400003, 0.0037200251049284037)
 20  (0.19630577057400003, 0.0037200251049284037)
 21  (0.19630577057400003, 0.0037200251049284037)
 22  (0.19802475385800003, 0.003729732907386365)
 23  (0.19802475385800003, 0.003729732907386365)
 24  (0.19802475385800003, 0.003729732907386365)
 25  (0.20139735178400003, 0.0038013853425120725)
 26  (0.20139735178400003, 0.0038013853425120725)
 27  (0.20139735178400003, 0.0038013853425120725)
 28  (0.183565045858, 0.00785041551470421)
 29  (0.183565045858, 0.00785041551470421)
 30  (0.183565045858, 0.00785041551470421)
 31  (0.18630153535800004, 0.00983805942407462)
 32  (0.18630153535800004, 0.00983805942407462)
 33  (0.18630153535800004, 0.00983805942407462)
 34  (0.175493558, 0.015891568527644367)
 35  (0.175493558, 0.015891568527644367)
 36  (0.175493558, 0.015891568527644367)
 37  (0.18790700285800002, 0.005994087304886936)
 38  (0.18790700285800002, 0.005994087304886936)
 39  (0.18790700285800002, 0.005994087304886936)
 40  (0.18769648535800001, 0.008272100044934779)
 41  (0.18769648535800001, 0.008272100044934779)
 42  (0.18769648535800001, 0.008272100044934779)
 43  (0.17501772300000001, 0.015862381099825622)
 44  (0.17501772300000001, 0.015862381099825622)
 45  (0.17501772300000001, 0.015862381099825622)
 46  (0.18602138650000002, 0.008838626869652661)
 47  (0.18602138650000002, 0.008838626869652661)
 48  (0.18602138650000002, 0.008838626869652661)
 49  (0.191920434926, 0.007069643880441053)
 50  (0.191920434926, 0.007069643880441053)
 51  (0.191920434926, 0.007069643880441053)
 52  (0.17895612500000002, 0.01445354575937078)
 53  (0.17895612500000002, 0.01445354575937078)
 54  (0.17895612500000002, 0.01445354575937078)
 55  (0.20497540000000003, 0.0034487192488427496)
 56  (0.20497540000000003, 0.0034487192488427496)
 57  (0.20497540000000003, 0.0034487192488427496)
 58  (0.19610460778400002, 0.003669679507067744)
 59  (0.19610460778400002, 0.003669679507067744)
 60  (0.19610460778400002, 0.003669679507067744)
 61  (0.21045274328400002, 0.00315676837298207)
 62  (0.21045274328400002, 0.00315676837298207)
 63  (0.21045274328400002, 0.00315676837298207)
 64  (0.203980957858, 0.0033604518057179754)
 65  (0.203980957858, 0.0033604518057179754)
 66  (0.203980957858, 0.0033604518057179754)
 67  (0.19267326771600002, 0.0037212364376619355)
 68  (0.19267326771600002, 0.0037212364376619355)
 69  (0.19267326771600002, 0.0037212364376619355)
 70  (0.21455177457400001, 0.002958605105657074)
 71  (0.21455177457400001, 0.002958605105657074)
 72  (0.21455177457400001, 0.002958605105657074)
 73  (0.211204899858, 0.0032135513096506136)
 74  (0.211204899858, 0.0032135513096506136)
 75  (0.211204899858, 0.0032135513096506136)
 76  (0.2022715095, 0.0036036151715261575)
 77  (0.2022715095, 0.0036036151715261575)
 78  (0.2022715095, 0.0036036151715261575)
 79  (0.21683018785799998, 0.002953025330805547)
 80  (0.21683018785799998, 0.002953025330805547)
 81  (0.21683018785799998, 0.002953025330805547)

'''


ap_albedo_region_surrounding = np.loadtxt('2020_AV2_albedo_around_region')

print np.median(ap_albedo_region_surrounding), np.std(ap_albedo_region_surrounding)

a_hist_bins_min = 0.1
a_hist_bins_max = 0.25
delta_a_hist_bins = 0.005
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(ap_albedo_region_surrounding,bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.median(ap_albedo_region_surrounding)#ap albedo

x_label = 'Albedo'
y_label = 'Per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_region_surrounding'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([n_hist_fraction])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#Gemini GNIRS XD data 2020-01-19

from specutils.wcs import specwcs
from astropy.io import fits
from astropy import units as u
from specutils.io import read_fits
myspec = read_fits.read_fits_spectrum1d('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200119/quick_look/PRODUCTS/2020AV2.fits')
plt.figure()
plt.plot(myspec.dispersion, myspec.flux)
plt.ylim(-1e-16,7e-16)
plt.savefig('PRODUCTS_2020AV2_2020_01_19.png')


#try generating your own spectrum from the *_comb.fits files


from specutils import Spectrum1D
from specutils.wcs import specwcs
from astropy.io import fits
from astropy import units as u

datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200119/quick_look/vtarget_comb.fits', header=True)
dat_header = datfile[1]
dat = datfile[0]

dispersion_start = dat_header['CRVAL1'] - (dat_header['CRPIX1'] - 1) * dat_header['CD1_1']
linear_wcs = specwcs.Spectrum1DPolynomialWCS(degree=1, c0=dispersion_start,c1=dat_header['CD1_1'],unit=u.Unit('angstrom'))
dat_spectrum = Spectrum1D(flux=dat, wcs=linear_wcs)
plt.figure()
plt.plot(dat_spectrum.dispersion, dat_spectrum.flux)
plt.ylim(-10,40.)
plt.savefig('2020_AV2_comb_2020_01_19.png')

standardfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200119/quick_look/vstandard_comb.fits', header=True)
sta_header = standardfile[1]
standard = standardfile[0]

dispersion_start = sta_header['CRVAL1'] - (sta_header['CRPIX1'] - 1) * sta_header['CD1_1']
linear_wcs = specwcs.Spectrum1DPolynomialWCS(degree=1, c0=dispersion_start,c1=sta_header['CD1_1'],unit=u.Unit('angstrom'))
sta_spectrum = Spectrum1D(flux=standard, wcs=linear_wcs)
plt.figure()
plt.plot(sta_spectrum.dispersion, sta_spectrum.flux)
plt.savefig('standard_comb_2020_01_19.png')

#Gemini GNIRS XD data 2020-01-20

from specutils.wcs import specwcs
from astropy.io import fits
from astropy import units as u
from specutils.io import read_fits
myspec = read_fits.read_fits_spectrum1d('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200120/quick_look/GN-2019B-DD-111_2/PRODUCTS/2020AV2.fits')
plt.figure()
plt.plot(myspec.dispersion, myspec.flux)
plt.ylim(-100e-16,57e-16)
plt.savefig('PRODUCTS_2020AV2_2020_01_20.png')


#try generating your own spectrum from the *_comb.fits files


from specutils import Spectrum1D
from specutils.wcs import specwcs
from astropy.io import fits
from astropy import units as u

datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200120/quick_look/GN-2019B-DD-111_2/vtarget_comb.fits', header=True)
dat_header = datfile[1]
dat = datfile[0]

dispersion_start = dat_header['CRVAL1'] - (dat_header['CRPIX1'] - 1) * dat_header['CD1_1']
linear_wcs = specwcs.Spectrum1DPolynomialWCS(degree=1, c0=dispersion_start,c1=dat_header['CD1_1'],unit=u.Unit('angstrom'))
dat_spectrum = Spectrum1D(flux=dat, wcs=linear_wcs)
plt.figure()
plt.plot(dat_spectrum.dispersion, dat_spectrum.flux)
plt.ylim(-800,800)
plt.savefig('2020_AV2_comb_2020_01_20.png')

standardfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/Gemini/20200120/quick_look/GN-2019B-DD-111_2/vstandard_comb.fits', header=True)
sta_header = standardfile[1]
standard = standardfile[0]

dispersion_start = sta_header['CRVAL1'] - (sta_header['CRPIX1'] - 1) * sta_header['CD1_1']
linear_wcs = specwcs.Spectrum1DPolynomialWCS(degree=1, c0=dispersion_start,c1=sta_header['CD1_1'],unit=u.Unit('angstrom'))
sta_spectrum = Spectrum1D(flux=standard, wcs=linear_wcs)
plt.figure()
plt.plot(sta_spectrum.dispersion, sta_spectrum.flux)
plt.savefig('standard_comb_2020_01_20.png')



'''

2020-01-15 / Mikael Granvik

Synthetic Vatiras (synthetic_vatiras_1000x.txt; cols:
a,e,i,node,argperi,meanano,H) is generated from a medium resolution
(da=0.05au, de=0.02, di=2.0deg, dH=0.25) version of the Granvik+ NEO
model (Granvik+ 2018, Icarus 312, 181-207) in the range
15<H<18. Oversampling by a factor of 1000, that is, 1168279 NEOs
generated out of which 2521 are Vatiras. Hence the model predicts 2.5
Vatiras with H<18. The model also predicts 0.34 Vatiras with H<16.4
which is the H mag of 2020 AV2, and thus suggests that it is the
largest Vatira out there.


'''

#AV2 elems:

a_off = 0.55536
da=0.05
e_off = 0.177296
de=0.02
i_off = 15.880
di=2.0
H_off = 16.3
dH=0.25

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T
vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T



'''
stat test
#3 sigma range expected

'''

#orbital element statistical tests

#found objects observations

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[i] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[i]]).T

#official elements
a_off = 0.55536
e_off = 0.177296
i_off = 15.880
H_off = 16.3
first_note = 'Synthetic population'
second_note = 'Synthetic population detected'
notes = np.array([first_note,second_note])
plt_dir='./'
ext = '.png'
#ytick_locs = np.arange(0,1.2,0.1)


#H hist
a_hist_bins_min = 15.0
a_hist_bins_max = 19.05
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = H_off
xlim_min,xlim_max = 14.5,19.0

x_label = 'H Magnitude'
file_name='synth_H_detected'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
data_sets = np.array([first_hist_values,second_hist_values])
make_hist_two_datasets_no_legend(xtick_locs, data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0

data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])

error_bar_high = data_sets[3] - data_sets[1]
error_bar_low = data_sets[1] - data_sets[2]


#uncertainty on completeness


#frank efficiencies

twilight_survey_stats_found_objects_fields_object_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(0,),dtype='string')

found_FieldID, found_FieldMJD, found_AstRange_km, found_AstRangeRate_kms, found_AstRA_deg, found_AstRARate_deg_day, found_AstDec_deg, found_AstDecRate_deg_day, found_Ast_Sun_x_km, found_Ast_Sun_y_km, found_Ast_Sun_y_km, found_Sun_Ast_Obs_deg, found_V, found_V_H_eq_0 = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_survey_vatiras_detected_2019_09_20_to_2020_1_30_observations',usecols=(1,2,3,4,5,6,7,8,9,10,11,12,13,13)).T

#objects simulator and found
twilight_survey_stats_all_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(0,),dtype='string',skiprows=1)

vat_all_a_au, vat_all_e, vat_all_i_deg, vat_all_Omega_deg, vat_all_omega_deg, vat_all_M_deg, vat_all_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras.des',usecols=(2,3,4,5,6,7,8),skiprows=1).T

twilight_survey_stats_detected_objects_names = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(0,),dtype='string')

vat_det_a_au, vat_det_e, vat_det_i_deg, vat_det_Omega_deg, vat_det_omega_deg, vat_det_M_deg, vat_det_H = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/granvik_2018_vatiras_detected.des',usecols=(2,3,4,5,6,7,8)).T


#frank efficiencies
frank_per_object_names = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(0,),dtype='string')

frank_per_object_efficiency = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/frank_vatira_efficiency_run/total_efficiency_per_vatira_NEWonly.txt',usecols=(1,))

#connect frank efficiencies to a,e,i,H

frank_a_e_i_H_efficiency = np.zeros(5*len(frank_per_object_efficiency)).reshape(len(frank_per_object_efficiency),5)
for i in range(0,len(frank_per_object_names)):
    index = np.where(twilight_survey_stats_detected_objects_names==frank_per_object_names[i])[0]
    frank_a_e_i_H_efficiency[i] = np.array([vat_det_a_au[index], vat_det_e[index], vat_det_i_deg[index], vat_det_H[index], frank_per_object_efficiency[i]]).T

#H hist

a_hist_bins_min = 15.0
a_hist_bins_max = 19.05
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

H_av = 16.2
H_off = 16.2
n=1#sigma on H mag
sigma_size = 0.8

#number of frank objeccts
print np.where(frank_a_e_i_H_efficiency[:,3] < (H_av+ (n*sigma_size)))[0].shape

#number of weighted frank objects per bin
print make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]

#completeness

print make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]

#uncertainties
print make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))])

print make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(-1.*np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))])


# mean completeness and uncertainties for H<H_av * 1 sigma

print np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]), np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))])), np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(-1.*np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]))


first_hist_values = make_histogram1d(vat_all_H,bins)
second_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/make_histogram1d(vat_all_H,bins)
second_hist_values[np.where(np.isnan(second_hist_values)==True)]=0.0
third_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
third_hist_values[np.where(np.isnan(third_hist_values)==True)]=0.0
fourth_hist_values = make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)/(-1.0 *np.sqrt(make_histogram1d(vat_all_H,bins)) + make_histogram1d(vat_all_H,bins))
fourth_hist_values[np.where(np.isnan(fourth_hist_values)==True)]=0.0

data_sets = np.array([first_hist_values,second_hist_values,third_hist_values,fourth_hist_values])

error_bar_high = data_sets[3] - data_sets[1]
error_bar_low = data_sets[1] - data_sets[2]

error_bar_average = (error_bar_low + error_bar_high)/2.0


#old way
completeness_nominal = np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))])

completeness_low = np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]))

completeness_high =  np.mean(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))]/(-1.*np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))]))

print "old way", completeness_nominal, completeness_low, completeness_high

#error weighted
completeness_nominal = error_weighted_average(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]/make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:] ,error_bar_average[np.where(bins < (H_av+ (n*sigma_size)))][1:])[0]

completeness_low = error_weighted_average(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]/(np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]),error_bar_average[np.where(bins < (H_av+ (n*sigma_size)))][1:])[0]


completeness_high = error_weighted_average(make_weighted_histogram(frank_a_e_i_H_efficiency[:,3],frank_a_e_i_H_efficiency[:,4],a_hist_bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]/(-1.*np.sqrt(make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]) + make_histogram1d(vat_all_H,bins)[np.where(bins < (H_av+ (n*sigma_size)))][1:]),error_bar_average[np.where(bins < (H_av+ (n*sigma_size)))][1:])[0]

print "new way", completeness_nominal, completeness_low, completeness_high


completeness_plus = completeness_high - completeness_nominal
completeness_minus = completeness_nominal - completeness_low

#number expected

number_in_range_high = np.where((vat_all_H < (H_av+ (n*sigma_size))) & (vat_all_H> bins[0]))[0].shape[0]/1000.
number_in_range_low = np.where((vat_all_H < (H_av+ (-1.*n*sigma_size))) & (vat_all_H> bins[0]))[0].shape[0]/1000.
number_in_range = np.where((vat_all_H < (H_av+ (0.0*n*sigma_size))) & (vat_all_H> bins[0]))[0].shape[0]/1000.


print "number_in_range, number_in_range_low, number_in_range_high"
print number_in_range, number_in_range_low, number_in_range_high


number_in_range_plus = (number_in_range_high - number_in_range)*1.0
number_in_range_minus = (number_in_range - number_in_range_low)*1.0


expected_number_detect_nominal = number_in_range * np.mean([completeness_high,completeness_low])

expected_number_high = number_in_range_high * np.mean([completeness_high,completeness_low])

expected_number_low = number_in_range_low * np.mean([completeness_high,completeness_low])

expected_plus = expected_number_high - expected_number_detect_nominal

expected_minus = expected_number_detect_nominal - expected_number_low


#mean number expected and uncertainties for H<16.3 * 1 sigma =0.5

#stat tests poisson
from scipy.stats import poisson

number_events = 1.0

#nominal range
print "nominal range"
print poisson.pmf(1., expected_number_detect_nominal, loc=0)

#low range
print "low range"
print poisson.pmf(1., expected_number_low, loc=0)

#high range
print "high range"
print poisson.pmf(1., expected_number_high, loc=0)


#print np.where(vat_all_H < (16.3+ (n*sigma_size)))

#95% confidence interval detection of 1 or more

prob_one_or_more_nominal = scipy.stats.poisson.pmf(1,expected_number_detect_nominal) + scipy.stats.poisson.pmf(2,expected_number_detect_nominal) + scipy.stats.poisson.pmf(3,expected_number_detect_nominal) + scipy.stats.poisson.pmf(4,expected_number_detect_nominal)

prob_one_or_more_low = scipy.stats.poisson.pmf(1,expected_number_low) + scipy.stats.poisson.pmf(2,expected_number_low) + scipy.stats.poisson.pmf(3,expected_number_low) + scipy.stats.poisson.pmf(4,expected_number_low)

prob_one_or_more_high = scipy.stats.poisson.pmf(1,expected_number_high) + scipy.stats.poisson.pmf(2,expected_number_high) + scipy.stats.poisson.pmf(3,expected_number_high) + scipy.stats.poisson.pmf(4,expected_number_high)

#integegrate over sigmas

num_sigmas = 2.0 #defines your confidence interval, in this case 2 sigma or 95%

sigma_low = expected_number_detect_nominal - expected_number_low
sigma_high = expected_number_high - expected_number_detect_nominal

number_sigmas = np.linspace(-1 * num_sigmas,num_sigmas,1000)

sigmas_low_and_high = number_sigmas

sigmas_low_and_high[np.where(number_sigmas<0)] *= sigma_low

sigmas_low_and_high[np.where(number_sigmas>0)] *= sigma_high

expected_number_mu = sigmas_low_and_high + expected_number_detect_nominal

expected_number_mu[np.where(expected_number_mu<0.0)] = 0.0

prob_one_or_more_integrate = np.ones(len(expected_number_mu))

for i in range(0,len(prob_one_or_more_integrate)):
    prob_one_or_more_integrate[i] = scipy.stats.poisson.pmf(1,expected_number_mu[i]) + scipy.stats.poisson.pmf(2,expected_number_mu[i]) + scipy.stats.poisson.pmf(3,expected_number_mu[i]) + scipy.stats.poisson.pmf(4,expected_number_mu[i])#could sum out longer if you want

print np.sum(prob_one_or_more_integrate *expected_number_mu)/np.sum(expected_number_mu)


'''
#objects expected 

assuming 0.3 H mag sigma error bars

number: 
0.253 +/- 0.092,0.138

completeness:
0.18042352646222526 +/- 0.01896262220748446, 0.02426149235877903

objects: 
0.04564715219494299 +/- 0.02599520558501327,0.08003184235901269 #1 sigma

#95 % confidence interval integrated probability
0.0688476759696

#assuming 0.5 H mag sigma error bars

number: 0.253 +/- 0.152,0.255

completeness:
0.1859903025311551 +/- 0.017842441588528823, 0.022418783169679185

objects: 
0.047055546540382236 +/- 0.030072612585176982,0.05881626899564158 #1 sigma

#95 % confidence interval integrated probability
0.0981244230035

#assuming 0.7 H mag sigma error bars

number: 0.253 +/- 

completeness:
0.185990302531 +/- 0.017842441587999996, 0.022418783170000012

objects:
0.0448926102425 +/- 0.035564580216449995, 0.07576328171749999

#95 % confidence interval integrated probability
0.134838390612


'''
#try to estimate per a,e,i,H bin completeness
#1 sigma
occurance = 1.0
sigma = np.sqrt(occurance)

number_of_objects_expected = 0.01
multiple_of_sigma = number_of_objects_expected/occurance

cum_prob = scipy.special.erf(multiple_of_sigma/np.sqrt(2))

print cum_prob


vat_all_a_au[np.where((vat_all_a_au<a_off+da)&(vat_all_a_au>a_off-da)&(vat_all_e<e_off+de)&(vat_all_e>e_off-de)&(vat_all_i_deg<i_off+di)&(vat_all_i_deg>i_off-di)&(vat_all_H<H_off+dH)&(vat_all_H>H_off-dH))]
vat_all_e[np.where((vat_all_a_au<a_off+da)&(vat_all_a_au>a_off-da)&(vat_all_e<e_off+de)&(vat_all_e>e_off-de)&(vat_all_i_deg<i_off+di)&(vat_all_i_deg>i_off-di)&(vat_all_H<H_off+dH)&(vat_all_H>H_off-dH))]
vat_all_i_deg[np.where((vat_all_a_au<a_off+da)&(vat_all_a_au>a_off-da)&(vat_all_e<e_off+de)&(vat_all_e>e_off-de)&(vat_all_i_deg<i_off+di)&(vat_all_i_deg>i_off-di)&(vat_all_H<H_off+dH)&(vat_all_H>H_off-dH))]
vat_all_H[np.where((vat_all_a_au<a_off+da)&(vat_all_a_au>a_off-da)&(vat_all_e<e_off+de)&(vat_all_e>e_off-de)&(vat_all_i_deg<i_off+di)&(vat_all_i_deg>i_off-di)&(vat_all_H<H_off+dH)&(vat_all_H>H_off-dH))]

vat_det_a_au[np.where((vat_det_a_au<a_off+da)&(vat_det_a_au>a_off-da)&(vat_det_e<e_off+de)&(vat_det_e>e_off-de)&(vat_det_i_deg<i_off+di)&(vat_det_i_deg>i_off-di)&(vat_det_H<H_off+dH)&(vat_det_H>H_off-dH))]
vat_det_e[np.where((vat_det_a_au<a_off+da)&(vat_det_a_au>a_off-da)&(vat_det_e<e_off+de)&(vat_det_e>e_off-de)&(vat_det_i_deg<i_off+di)&(vat_det_i_deg>i_off-di)&(vat_det_H<H_off+dH)&(vat_det_H>H_off-dH))]
vat_det_i_deg[np.where((vat_det_a_au<a_off+da)&(vat_det_a_au>a_off-da)&(vat_det_e<e_off+de)&(vat_det_e>e_off-de)&(vat_det_i_deg<i_off+di)&(vat_det_i_deg>i_off-di)&(vat_det_H<H_off+dH)&(vat_det_H>H_off-dH))]
vat_det_H[np.where((vat_det_a_au<a_off+da)&(vat_det_a_au>a_off-da)&(vat_det_e<e_off+de)&(vat_det_e>e_off-de)&(vat_det_i_deg<i_off+di)&(vat_det_i_deg>i_off-di)&(vat_det_H<H_off+dH)&(vat_det_H>H_off-dH))]


'''
#neptunian trojan exposure times

print 23.5, 1198.20215292 + 494.217021106 + 2402.26138788 + (((1198.20215292 + 494.217021106 + 2402.2613878)/120. )*7.5) ,23, 494.133640007 + 203.813066934 + 892.457216142, 22.6, 619.388030082 + 295.604643892 + 121.926710091 + 619.388030082,22.0,209.253518788 + 180.40178461 + 74.4095079242 


'''


#exposure times for SNR 15 with WASP

#g 23.5 1198.2021529160331 23 494.1336400070613 22.6 295.60464389220914 22.3 180.40178461024283
#r 23.5 494.217021106 23 203.813066934 22.6 121.926710091 22.3 74.4095079242
#i 23.5 2402.26138788 23 892.457216142 22.6 619.388030082 22.0 209.253518788

#taking into account overheads

print 23.5, 1198.20215292 + 494.217021106 + 2402.26138788 + (((1198.20215292 + 494.217021106 + 2402.2613878)/120. )*7.5) ,23, 494.133640007 + 203.813066934 + 892.457216142 +(((494.133640007 + 203.813066934 + 892.457216142)/120. )*7.5), 22.6, 295.604643892 + 121.926710091 + 619.388030082 + (((295.604643892 + 121.926710091 + 619.388030082)/120. )*7.5),22.0, 180.40178461 + 74.4095079242 + 209.253518788 + (((180.40178461 + 74.4095079242 + 209.253518788)/120. )*7.5)


'''

23.5 4350.59809702 23 1689.80416828 22.6 1101.72684557 22.0 493.06886203


'''


print 23.5, 4350.59809702/60., 23, 1689.80416828/60., 22.6, 1101.72684557/60., 22.0, 493.06886203/60.

'''
23.5 72.5099682837 23 28.1634028047 22.6 18.3621140928 22.0 8.21781436717


'''


#plots showing solar elongation angle hist per day all Twilight Survey.

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse_test.txt',skiprows=1).T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)
only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params

fov_size = 3.7081 * 2

a_hist_bins_min = 35.
a_hist_bins_max = 65.0
delta_a_hist_bins = 0.6125
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



plt.figure()
plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]


xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs
vline_loc_2 = 48.7

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_all'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

'''
In [214]: n_hist_fraction.mean(0)[:10].sum()
Out[214]: 0.27453540731117304

27.5 percent of pointings with Sun-centric distnace < 46 degrees

'''


#plots showing solar elongation angle hist per day 2019 September 20 UTC to 2020 January 30 UTC.

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('survey_simulation_objects_in_field_standard_install/objectsInField-master/main/twilight_since_2019_09_20_reverse.txt',skiprows=1)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>58742)].T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)
only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params

a_hist_bins_min = 35.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if j % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180-4)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



plt.figure()
plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]


xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_September_20_to_2020_January_30'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([n_hist_fraction.mean(0)])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


'''
#n_grid check
fig = plt.figure(figsize=(24,10))
plt.imshow(n_grid[::-1,::-1], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric Ecliptic Longitude [deg]",fontsize="30")
plt.ylabel("Sun-centric Ecliptic Latitude [deg]",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
'''

#recompute survey simlations using different phase functions, S, C, V, maybe others? and send to Frank

'''

From Frank

2020 July 8


 As before, I’ve computed the overall detection efficiencies per synthetic Vatira across all 100 successful ZMODE_lite runs (2019/09/20 to 2020/01/30) for each G parameter.

The efficiency per object is e_i = 1 - PROD_j{1 - e_ij} over all 100 runs: j = 0…100.

The four “total_efficiency_per_vatira*.txt.gz” files contain these efficiencies. At the end of these files are some other statistics.
 
The numbers we should have detected (i.e., sum_i{e_i}) are now:
G = 0.10 :  509.400 / 2522 ~ 0.2019
G = 0.20 :  511.617 / 2522 ~ 0.2028
G = 0.25 :  512.374 / 2522 ~ 0.2031
G = 0.45 :  514.243 / 2522 ~ 0.2039
I.e., not too different from before.



'''


'''
2020 AV2 H mag estimate

'''

#


#quick and dirty mag estimate

av2_r_mag = -7.65

av2_r_mag_unc = 0.07

star_r_mag = -9.4699


'''
mag look up

panstarrs_query_g_r_i_z_gmr_rmi_imz(321.21683,-6.1314211,10/3600.)

'''
star_r_catalog_mag =16.242

ZTF_r_mag = av2_r_mag - star_r_mag +star_r_catalog_mag
m_AV2_Vmr = 0.32

MLO_R_mag = 18.0#from MLO photometry
m_AV2_VmR = 0.48


H_magztfhg = ZTF_r_mag- phaseFunction( np.radians(86.3834),0.15, 0.627183311332, 0.79781750789183, 'HG')+m_AV2_Vmr

MLO_R_mag = 17.9#from MLO photometry
m_AV2_VmR = 0.45

H_magmlhg = ZTF_r_mag- phaseFunction( np.radians(98.4491),0.15, 0.593945650952, 0.70149750351463, 'HG')+m_AV2_VmR

#HG12

H_magztfhg12 = ZTF_r_mag- phaseFunction( np.radians(86.3834),0.5361873077200001, 0.627183311332, 0.79781750789183, 'HG12')+m_AV2_Vmr

MLO_R_mag = 18.0#from MLO photometry
m_AV2_VmR = 0.48

H_magmlohg12 = ZTF_r_mag- phaseFunction( np.radians(98.4491),0.5361873077200001, 0.593945650952, 0.70149750351463, 'HG12')+m_AV2_VmR


'''
solar angle range for mercury
In [7]: np.degrees(np.arcsin(0.307499/1.)), np.degrees(np.arcsin(0.466697/1.))
Out[7]: (17.90857308143162, 27.820104370940513)

'''

'''
2020-01-04 obs were at airmass ~2.3

also show the sun-centric pointing distirbution from the P48 proposal

'''

#limiting magnitude vs time plots for twilight survey

lim_mag_date_JD, lim_mag_diff_mag_r = np.loadtxt('TwilightSurvMagLims_date_time_JD_airmass_diff_mag_lim_sci_mag_lim.txt',usecols=(2,4)).T

'''
inner-Mercurian belt object integrations

From Morby, 2020 July 10:

To study the lickage of bodies from putative belts between mercury and
Venus or inside of the orbt of Mercury, one has just o select initial
conditions that ar eno planet corssers and integrate them with all the
planets for sufficiently logntime. The problem is the timestep. You need
to chose at least 1/50 of the shortest orbital period.... So, it's
intense. The good news is that you can use the swift_mvs code which is
faster, stopping particles when they have close encounters with the
planets. You will then restart them with rmvs3 to measure their
residence time in the Atira region to deduce from the number of Atiras
the actual escape rate needed. The issue is how to interpret the
results, i.e. how to estimate the population that is needed to get the
required escape rate. Some particles may escape very rapidly because
they are in resonances or come too close to the planets even if not
crossers at the beginning. Then hopefully the escape rate will stabilize
to something roughly constant for a while, then decay to almost 0 as
only stable particles remain. I guess we can then argue that the regions
with an intermediate escape rate are repopulated by collisions/Yarkovsky
and therefore they are representative of the real escape rate.

cheers

Morby

From Bolin, 2020 July 10:

Hello Morby,

Thank you for the a<1 au NEOs and clarifying the last column.

The plan you have for simulating the linkage of bodies from inner
Venus belts sounds pretty straightforward. Do you think it is feasible
to use symplecticity-conserving, adaptive integrators such as the
IAS15 by Hanno Rein:

https://arxiv.org/abs/1409.4779

It apparently can accurately simulate particles that undergo close
encounters over many orbits, maybe something that can be useful for
us? This is what I used before to study the orbital evolution of 2020
AV2.

How many particles would you recommend running for this? I can ask
around here what kind of HPC access I have. Maybe Konstantin can help
with access.

Thank you,

Bryce

From Morby 2020 July 10:

Hi

sure, that's also a good integrator. I have never used it though.

I would start with 1,000 particles to see where the regions that have
long-term stability are. Then increase the resolution in those regions.

maybe some Master student of Konstantin could do the job.

cheers

Morby

From 2020 July 10:

Hello,

OK, this sounds good, though from what it sounds I will need to use
the HPC for the 1000 particles at 1/50 orbital time period time steps.

This does sound like a master student's work, but maybe something I
can try to do myself if it is not too involved. I just wrote to
Konstantin asking about HPC so we will see.

Cheers,

Bryce


From Bolin 2020 July 10:

Hello Konstantin,

Morby and I are looking to integrate ~1,000 particles with time steps
~1/50 the orbital period inside the orbit of Mercury from 0.1-0.2 au
with at least the terrestrial planets. We think that using the IAS15
adaptive integrator with REBOUND could do the job.

I have no feeling for how many cores and time per core are required to
do this batch of simulations, but I think it is going to take more
than my laptop.

Do you know if there are HPC resources with REBOUND already set up on
it at Caltech that I could access to do this kind of simulation?

Thank you,

Bryce

From Konstantin 2020 July 10:

Hi Bryce,

I’m really not sure about the HPC resources, but I doubt it has REBOUND pre-installed. That said, REBOUND installation is trivial (even I could do it). Also, how long are you looking to integrate for? My sense is that if your integration timescale is ~100Myr or less, just break it up into runs with ~130 particles each and that is something that can be integrated in a week or two (I’m guessing) on a 8-core laptop.

Cheers,
K

From Bolin 2020 July 10:

Hello Konstatin,

Your idea to use an 8 core laptop sounds feasible at this stage of
~1000 particles for an initial test of determining stability regions
before going towards higher-resolution simulations for the next stage.

Morby, does running this simulation for ~100 Myrs sound reasonable to
you for determining the stability regions from which to later use in
simulations for measuring the escape rate from the putative belts?

Thank you,

Bryce

From Morby 2020 July 10:

Hi

yes, starting with 100My is a good thing. I don't think you need HPC.
the problem is trivially parallelized by dividing the 1,000 particle
batch in 4 of 250 each or 10 of 100 each. Dont'put less than that,
because there are 8 planets to integrate this time and if you don't have
enough particles it's a waist in resources.

Cheers

Morby


'''


#update the twilight survey pointings solar elongation plot

'''
awk '{printf "%s %.7f %.7f %.7f %s'\\n'",$'10',$'5',$'6',$'4'-2400000.500000,"0.0"}' < All_twilight_obs_updated_2020_Nov_17.txt >twilight_since_2019_09_20_updated_2020_Nov_17.txt

tail -r twilight_since_2019_09_20_updated_2020_Nov_17.txt > twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt
'''

#plots showing solar elongation angle hist per day 2019 September 20 UTC to 2020 January 30 UTC in 4 exposures per field cadence

#morning
only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>58742) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<58880))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start>.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_September_20_to_2020_January_30_morning'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#evening

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>58742) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<58880))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start<.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_September_20_to_2020_January_30_evening'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#plots showing solar elongation angle hist per day 2019 September 20 UTC to 2020 August 1 UTC in 4 exposures per field cadence to end of 4 exposure per field cadence

#morning

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>58742) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<59062))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start>.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_September_20_to_2020_August_1_morning'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#evening

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>58742) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<59062))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start<.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_September_20_to_2020_August_1_evening'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#plots showing solar elongation angle hist per day 2020 August 1 UTC to 2020 November 17 UTC in 5 exposures per field cadence


#Solar distance of 2020 AV2 at time of discovery is 39.8120584748 degrees
#New 5 x 10 mode results in 10% fewer fields in the Vatira zone

#morning

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>59062) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<59170))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start>.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start>.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_August_1_to_2020_November_17_morning'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#evening

only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg = np.loadtxt('twilight_since_2019_09_20_updated_2020_Nov_17_reverse.txt',skiprows=1)

only_new_field_id_start, only_new_field_ra_deg_start, only_new_field_dec_deg_start, only_new_field_mjd_start, only_new_angle_deg_start = only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[np.where((only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]>59062) &(only_new_field_id_only_new_field_ra_deg_only_new_field_dec_deg_only_new_field_mjd_only_new_angle_deg[:,-2]<59170))].T

only_new_field_mjd_decimal_start, only_new_field_mjd_day_start = np.modf(only_new_field_mjd_start)

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_angle_deg = only_new_field_id_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_ra_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_dec_deg_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_field_mjd_start[np.where(only_new_field_mjd_decimal_start<.5)], only_new_angle_deg_start[np.where(only_new_field_mjd_decimal_start<.5)]

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2019_August_1_to_2020_November_17_evening'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#plot simulation results for 2 x 5 x 5  and 5 x 10

#sims for 2 x 5 x 5

#morning

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31_double').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_morning_double_chunk'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#evening

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31_double').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_evening_double_chunk'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#seasonal plots showing min and max elongation as a function of time

#morning

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31_double').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.legend(loc='lower right',prop={'size':12})
plt.savefig('twilight_survey_morning_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_double_chunk.png')


#evening

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31_double').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.legend(loc='lower right',prop={'size':12})
plt.savefig('twilight_survey_evening_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_double_chunk.png')

# sims for 5 x 10

#morning

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_morning_single_chunk'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#evening

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_evening_single_chunk'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#seasonal plots showing min and max elongation as a function of time

#morning

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
plt.legend(loc='lower right',prop={'size':12})
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.savefig('twilight_survey_morning_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_single_chunk.png')


#evening

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.legend(loc='lower right',prop={'size':12})
plt.savefig('twilight_survey_evening_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_single_chunk.png')


#plots for double chunk 6 fields x 4 visits

#morning

#source source_morning_double_x4

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31_double_x_4').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_morning_double_chunk_x_4_visits'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#evening

#source source_evening_double_x4

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31_double_x_4').T


only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = 3.7081 * 2

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)



#plt.figure()
#plt.plot(n_hist.sum(0),'.')


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs

x_label = 'Sun-centric distance [deg]'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='twilight_survey_sun_centric_distance_mean_per_session_per_bin_fraction_density_2020_January_1_to_2020_December_31_evening_double_chunk_x_4_visits'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_vline(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#seasonal plots showing min and max elongation as a function of time

#morning

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_morning_2020_Jan_1_to_2020_Dec_31_double_x_4').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.legend(loc='lower right',prop={'size':12})
plt.savefig('twilight_survey_morning_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_double_chunk_x_4_visits.png')


#evening

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd, only_new_solar_elong_angle_deg, only_new_airmass = np.loadtxt('twilight_survey_fields_evening_2020_Jan_1_to_2020_Dec_31_double_x_4').T

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

min_store = np.zeros(len(only_new_field_mjd_day_unique))
max_store = np.zeros(len(only_new_field_mjd_day_unique))


for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    #in_only_new_solar_elong_angle_deg = only_new_solar_elong_angle_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    #min_store[jj] = np.min(in_only_new_solar_elong_angle_deg)
    #max_store[jj] = np.max(in_only_new_solar_elong_angle_deg)
    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        if j % 100 ==0.0:
            print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    min_store[jj] = np.min(solar_dist_storage)
    max_store[jj] = np.max(solar_dist_storage)

indices = np.where(min_store<34)[0]
for i in range(0,len(indices)):
    min_store[indices[i]] = np.random.uniform(0,1) *0.5 +34
plt.figure()
plt.plot(max_store,label='Maximum Solar elongation')
plt.plot(min_store,label='Minimum Solar elongation')
plt.ylabel('Solar Elongation (degrees)')
plt.xlabel('Days Since 2020 January 1 UTC')
plt.xlim(0,365)
plt.ylim(25,60)
hline_loc=np.degrees(np.arcsin(0.718/1.0))
plt.axhline(hline_loc, color="black",linestyle='--')
plt.legend(loc='lower right',prop={'size':12})
plt.savefig('twilight_survey_evening_min_and_max_solar_elongation_2020_Jan_1_to_2020_Dec_31_double_chunk_x_4_visits.png')


#P60 recovery on 2020 Nov 24

datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_18_58_f_b_a_2020AV2_r_r.fits', header=True)
dat_raw = datfile[0]#[::-1,:] #must flip data then flip back
dat_head = datfile[1]
write_stack_array = dat_raw.astype(np.float32)
pyfits.writeto('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_18_58_f_b_a_2020AV2_r_r_32_bit.fits',write_stack_array,overwrite=True,header=dat_head)

datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_21_41_f_b_a_2020AV2_r_r.fits', header=True)
dat_raw = datfile[0]#[::-1,:] #must flip data then flip back
dat_head = datfile[1]
write_stack_array = dat_raw.astype(np.float32)
pyfits.writeto('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_21_41_f_b_a_2020AV2_r_r_32_bit.fits',write_stack_array,overwrite=True,header=dat_head)


datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_24_36_f_b_a_2020AV2_r_r.fits', header=True)
dat_raw = datfile[0]#[::-1,:] #must flip data then flip back
dat_head = datfile[1]
write_stack_array = dat_raw.astype(np.float32)
pyfits.writeto('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_24_36_f_b_a_2020AV2_r_r_32_bit.fits',write_stack_array,overwrite=True,header=dat_head)


datfile = pyfits.getdata('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_27_19_f_b_a_2020AV2_r_r.fits', header=True)
dat_raw = datfile[0]#[::-1,:] #must flip data then flip back
dat_head = datfile[1]
write_stack_array = dat_raw.astype(np.float32)
pyfits.writeto('/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_data/SEDM/rc20201124_13_27_19_f_b_a_2020AV2_r_r_32_bit.fits',write_stack_array,overwrite=True,header=dat_head)



'''
2020 AV2 close encounters

need to look at moid plots for 200 K run to interpret resonance hopping
'''


'''
2020 AV2 collision sims

'''

'''
Get 2020 AV2 six vector and 1 sigmas From Horizons
2459179.500000000 = A.D. 2020-Nov-26 00:00:00.0000 TDB 
XYZ   : -1.203535703788029E-01   5.139197230912179E-01   1.472135627335251E-01  -2.193135413081762E-02  -8.833205382450249E-03  -1.763897985200532E-03
sigmas:         6.09349177E-06          4.90609345E-06          1.03794211E-06          4.23479209E-08          3.60298765E-07          1.19171332E-07
'''

import rebound
import numpy as np
import os
#2020 AV2 collision test
#Configuration on 2020 Nov 26
au_to_meters = 149597870700.0
#generate random 6 vector for AV2, taken from JPL horizons in SS Barycentric coordiantes, solution date   	  2020-Dec-01 05:19:21
av_x, av_y, av_z, av_vx, av_vy, av_vz = -0.120353570379,0.513919723091,0.147213562734,-1.27492314714,-0.513495789563,-0.10253969532#velocities are in units au/yr * 1/(2*pi)
convert_to_au_yr_2_pi = 365.25/(2*np.pi)
d_av_x, d_av_y, d_av_z, d_av_vx, d_av_vy, d_av_vz = 5.32879407E-06,4.16283781E-06,1.00852905E-06, 3.87517203E-08*convert_to_au_yr_2_pi,3.09194942E-07*convert_to_au_yr_2_pi,9.82364608E-08*convert_to_au_yr_2_pi
n_av_x, n_av_y, n_av_z, n_av_vx, n_av_vy, n_av_vz = np.random.normal(av_x, d_av_x, 1), np.random.normal(av_y, d_av_y, 1), np.random.normal(av_z, d_av_z, 1), np.random.normal(av_vx, d_av_vx, 1), np.random.normal(av_vy, d_av_vy, 1), np.random.normal(av_vz, d_av_vz, 1)

def setupSimulation():
    sim = rebound.Simulation()
    sim.dt = 0.03 #365.256 * (0.05/(np.pi * 2)) = 2.906614894698666 days or < 88 days/30, 1/30th of mercury's orbit
    sim.integrator = "ias15" # IAS15 is the default integrator, so we don't need this line
    #planetary ephemerides on 2020 Nov 26
    sim.add(m=1.0, x=-0.00640126632276, y=0.00620517249001, z=9.78606886587e-05, vx=-0.000409325395383, vy=-0.000315412641671, vz=1.26662330043e-05,r=(6.957e8/au_to_meters))
    sim.add(m=1.66011415305e-07, x=-0.39952723051, y=-0.117148272195, z=0.0260798704536, vx=0.150199095611, vy=-1.49030743579, vz=-0.135558943088,r=(2.4397e6/au_to_meters))
    sim.add(m=2.44783828778e-06, x=-0.720873549119, y=0.081549348323, z=0.0423611434304, vx=-0.130192501828, vy=-1.17488475597, vz=-0.00861689461198,r=(6.0518e6/au_to_meters))
    sim.add(m=3.04043264802e-06, x=0.427771861617, y=0.892491003389, z=5.50709530973e-05, vx=-0.91486163747, vy=0.435901629503, vz=-5.28439605863e-06,r=(6.371e6/au_to_meters))
    sim.add( m=3.22715603755e-07, x=1.01065377597, y=1.0582601869, z=-0.00280485636824, vx=-0.554338571914, vy=0.634420977735, vz=0.0269030285218,r=(3.3895e6/au_to_meters))
    sim.add(m=0.000954791915211, x=2.8158710368, y=-4.25111133111, z=-0.0453635008441, vx=0.360313733959, vy=0.26303697176, vz=-0.00915176501251 ,r=(69.911e6/au_to_meters))
    sim.add(m=0.000285885672722, x=5.32606678503, y=-8.44438014262, z=-0.0652115658445, vx=0.25623433683, vy=0.172196013332, vz=-0.0131977351733 ,r=(58.232e6/au_to_meters))
    sim.add(m=0.0, x=n_av_x, y=n_av_y, z=n_av_z, vx=n_av_vx, vy=n_av_vy, vz=n_av_vz,r=(1.5e3/au_to_meters))
    #nominal orbit
    #sim.add(m=0.0, x=av_x, y=av_y, z=av_z, vx=av_vx, vy=av_vy, vz=av_vz,r=(1.5e3/au_to_meters))
    sim.move_to_com()
    return sim
sim = setupSimulation()
sim.collision = "direct"
num_years = 3e7
try:
    sim.integrate(num_years*2.*np.pi)
except rebound.Collision:
    collided = []
    for p in sim.particles:
        if p.lastcollision == sim.t:
            collided.append(p.index)
try:
    print("Particles {0} collided".format(collided), (sim.t/(2.*np.pi)), " years")
except NameError:
    print ("No collisions after ", (sim.t/(2.*np.pi)), " years")
#jjj = 5
#while(jjj>0):
#    os.system('say -v "Amelie" "simulation terminée"')

'''
#continuation

'''

num_years = 4.5e7
try:
    sim.integrate(num_years*2.*np.pi)
except rebound.Collision:
    collided = []
    for p in sim.particles:
        if p.lastcollision == sim.t:
            collided.append(p.index)
try:
    print("Particles {0} collided".format(collided), (sim.t/(2.*np.pi)), " years")
except NameError:
    print ("No collisions after ", (sim.t/(2.*np.pi)), " years")


'''
#collision log
NOMINAL ORBIT:
('Particles [3, 7] collided', 24816174.754363425, ' years')

CLONE ORBITS:
('No collisions after ', 55000000.0, ' years')
('No collisions after ', 55000000.0, ' years')
('No collisions after ', 55000000.0, ' years')
('No collisions after ', 55000000.0, ' years')
('No collisions after ', 55000000.0, ' years')
('No collisions after ', 49999999.99999999, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 30000000.0, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('No collisions after ', 24999999.999999996, ' years')
('Particles [4, 7] collided', 26189501.550007068, ' years')
('Particles [3, 7] collided', 2703460.4671834186, ' years')
('Particles [3, 7] collided', 2932411.071980557, ' years')
('Particles [3, 7] collided', 4827253.731706555, ' years')
('Particles [3, 7] collided', 4865891.138383026, ' years')
('Particles [3, 7] collided', 5363864.166559344, ' years')
('Particles [3, 7] collided', 5717748.120991281, ' years')
('Particles [3, 7] collided', 7286298.009122687, ' years')
('Particles [3, 7] collided', 12123159.193336891, ' years')
('Particles [3, 7] collided', 13512226.307386301, ' years')
('Particles [3, 7] collided', 14266623.05276026, ' years')
('Particles [3, 7] collided', 14458801.97060194, ' years')
('Particles [3, 7] collided', 13017115.882790746, ' years')
('Particles [3, 7] collided', 14662827.949896384, ' years')
('Particles [3, 7] collided', 16314329.521979695, ' years')
('Particles [3, 7] collided', 18364483.816110343, ' years')
('Particles [3, 7] collided', 1975062.2126166404, ' years')
('Particles [3, 7] collided', 22051783.459770523, ' years')
('Particles [3, 7] collided', 27931251.747133262, ' years')
('Particles [3, 7] collided', 31825962.809428282, ' years')
('Particles [3, 7] collided', 42427529.93524169, ' years')
('Particles [3, 7] collided', 44879593.74397194, ' years')
('Particles [2, 7] collided', 13446211.060977316, ' years')
('Particles [2, 7] collided', 18034337.328942202, ' years')
('Particles [2, 7] collided', 18358330.115600727, ' years')
('Particles [2, 7] collided', 18759434.528542586, ' years')
('Particles [2, 7] collided', 2169626.4080588156, ' years')
('Particles [2, 7] collided', 23123434.32556562, ' years')
('Particles [2, 7] collided', 2555204.7281664773, ' years')
('Particles [2, 7] collided', 2912061.4544594022, ' years')
('Particles [2, 7] collided', 29197798.226550017, ' years')
('Particles [2, 7] collided', 3600495.8393466244, ' years')
('Particles [2, 7] collided', 3912595.5372845135, ' years')
('Particles [2, 7] collided', 19494229.00425435, ' years')
('Particles [2, 7] collided', 4238479.981726818, ' years')
('Particles [2, 7] collided', 4280450.965238863, ' years')
('Particles [2, 7] collided', 4577008.53612033, ' years')
('Particles [2, 7] collided', 4714886.7135724295, ' years')
('Particles [2, 7] collided', 4815959.735042544, ' years')
('Particles [2, 7] collided', 22443701.793629475, ' years')
('Particles [2, 7] collided', 4785557.830304909, ' years')
('Particles [2, 7] collided', 5136896.094519445, ' years')
('Particles [2, 7] collided', 5359323.967396112, ' years')
('Particles [2, 7] collided', 5419798.042076607, ' years')
('Particles [2, 7] collided', 5442693.165518421, ' years')
('Particles [2, 7] collided', 5463556.320061764, ' years')
('Particles [2, 7] collided', 5779036.962836768, ' years')
('Particles [2, 7] collided', 5889849.567109242, ' years')
('Particles [2, 7] collided', 6145810.164220687, ' years')
('Particles [2, 7] collided', 6795218.758395185, ' years')
('Particles [2, 7] collided', 6899169.252491928, ' years')
('Particles [2, 7] collided', 3634455.529420813, ' years')
('Particles [2, 7] collided', 7072546.873156107, ' years')
('Particles [2, 7] collided', 711519.6344252775, ' years')
('Particles [2, 7] collided', 7167077.526852234, ' years')
('Particles [2, 7] collided', 7206379.692338148, ' years')
('Particles [2, 7] collided', 7374922.20049767, ' years')
('Particles [2, 7] collided', 7417652.702089046, ' years')
('Particles [2, 7] collided', 7435068.675325105, ' years')
('Particles [2, 7] collided', 7711397.884218443, ' years')
('Particles [2, 7] collided', 7969000.791960719, ' years')
('Particles [2, 7] collided', 8023013.360264533, ' years')
('Particles [2, 7] collided', 8359301.857857659, ' years')
('Particles [2, 7] collided', 8608880.45227036, ' years')
('Particles [2, 7] collided', 8393189.891929481, ' years')
('Particles [2, 7] collided', 8981613.751644393, ' years')
('Particles [2, 7] collided', 9749858.589381704, ' years')
('Particles [2, 7] collided', 10070519.824095529, ' years')
('Particles [2, 7] collided', 15785608.952622378, ' years')
('Particles [2, 7] collided', 10927055.293646347, ' years')
('Particles [2, 7] collided', 1171699.6072854064, ' years')
('Particles [2, 7] collided', 11156515.731610918, ' years')
('Particles [2, 7] collided', 12213878.3151757, ' years')
('Particles [2, 7] collided', 13114430.86209958, ' years')
('Particles [2, 7] collided', 13334771.236919867, ' years')
('Particles [2, 7] collided', 13779936.245548273, ' years')
('Particles [2, 7] collided', 14023554.640974373, ' years')
('Particles [2, 7] collided', 1428076.045140188, ' years')
('Particles [2, 7] collided', 14494557.891380876, ' years')
('Particles [2, 7] collided', 14871067.477333535, ' years')
('Particles [2, 7] collided', 15297697.061235433, ' years')
('Particles [2, 7] collided', 1535795.0257216087, ' years')
('Particles [2, 7] collided', 17439184.03406741, ' years')
('Particles [2, 7] collided', 17829114.484638, ' years')
('Particles [2, 7] collided', 1856277.2239781998, ' years')
('Particles [2, 7] collided', 1913673.4654525302, ' years')
('Particles [2, 7] collided', 1916942.7126496867, ' years')
('Particles [2, 7] collided', 20746937.360438164, ' years')
('Particles [2, 7] collided', 20946021.29263572, ' years')
('Particles [2, 7] collided', 21424446.654664192, ' years')
('Particles [2, 7] collided', 21795304.546488203, ' years')
('Particles [2, 7] collided', 21851458.659007132, ' years')
('Particles [2, 7] collided', 22299260.979993235, ' years')
('Particles [2, 7] collided', 23124011.950862024, ' years')
('Particles [2, 7] collided', 23374071.77408357, ' years')
('Particles [2, 7] collided', 24847041.469205417, ' years')
('Particles [2, 7] collided', 26311191.368169215, ' years')
('Particles [2, 7] collided', 27089506.405303635, ' years')
('Particles [2, 7] collided', 28072875.609409798, ' years')
('Particles [2, 7] collided', 2845929.8111357396, ' years')
('Particles [2, 7] collided', 29852077.932493407, ' years')
('Particles [2, 7] collided', 34956440.39392027, ' years')
('Particles [2, 7] collided', 36155112.904416434, ' years')
('Particles [2, 7] collided', 3918606.3955374886, ' years')
('Particles [2, 7] collided', 4596076.708343834, ' years')
('Particles [2, 7] collided', 9459059.327842511, ' years')
('Particles [1, 7] collided', 9419225.127341954, ' years')
('Particles [1, 7] collided', 5722697.335781679, ' years')
('Particles [1, 7] collided', 74551.83621523004, ' years')
('Particles [1, 7] collided', 8289863.427229988, ' years')
('Particles [1, 7] collided', 13496162.612337692, ' years')
('Particles [1, 7] collided', 15906276.214586949, ' years')
('Particles [1, 7] collided', 17697077.67290264, ' years')
('Particles [1, 7] collided', 25793099.62484484, ' years')
('Particles [1, 7] collided', 17697077.67290264, ' years')
('Particles [1, 7] collided', 33976509.50273355, ' years')
('Particles [1, 7] collided', 3681193.7664024527, ' years')
('Particles [1, 7] collided', 41866133.53662394, ' years')
('Particles [1, 7] collided', 4627215.869907991, ' years')
('Particles [1, 7] collided', 4885922.017571855, ' years')
('Particles [1, 7] collided', 14660851.667061646, ' years')
('Particles [1, 7] collided', 759812.411223843, ' years')
('Particles [1, 7] collided', 2585002.830538982, ' years')
('Particles [0, 7] collided', 20312680.90865534, ' years')
('Particles [0, 7] collided', 7580132.043053312, ' years')
('Particles [0, 7] collided', 6453779.69592304, ' years')
('Particles [0, 7] collided', 7714815.0258879345, ' years')
('Particles [0, 7] collided', 7801742.252045981, ' years')
('Particles [0, 7] collided', 13689989.443316191, ' years')
('Particles [0, 7] collided', 13809397.692451032, ' years')
('Particles [0, 7] collided', 17105331.400831584, ' years')
('Particles [0, 7] collided', 21018190.824568648, ' years')
('Particles [0, 7] collided', 23760098.368492484, ' years')
('Particles [0, 7] collided', 32013204.70644184, ' years')
('Particles [0, 7] collided', 32193754.99155753, ' years')
('Particles [0, 7] collided', 53658698.278738506, ' years')
('Particles [0, 7] collided', 51498126.549348995, ' years')

'''


'''
close encounters 200 Kyr run

hi res 

Minimum distance (0.000042 AU) occured at time: -193027.509651 years.
Minimum distance (0.011312 AU) occured at time: -182166.699108 years.

coarser 
Minimum distance (0.001749 AU) occured at time: -139428.889905 years.
Minimum distance (0.044380 AU) occured at time: -199410.633235 years.


see plots moid_mercury_200_Kyr and moid_venus_200_Kyr

'''

#Ayló'chax Poşwáamayian albedo distribution from Morbideli et al. 2020

at_a_au, at_e, at_i, at_H, at_albedo = np.loadtxt('morby_synthetic-Atiras.dat').T

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.1
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo),bins)

plt.figure()
plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.22)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative mean per survey session, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_distribution'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#number albedo distribution

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<18.0)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*2.4



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cummulative_number_distribution'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#cumulative fraction

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<18.0)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*1.0



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative fraction'
file_name='morbidelli_aylochax_poswaamay_albedo_cummulative_fraction_distribution'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#albedo cumulative H lt 16.4


ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<16.4)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.3



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_16_4'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#albedo cumulative H lt 15.6

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<15.6)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.1



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_15_6'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#albedo cumulative H lt 17.2

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<17.2)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.9



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_17_2'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#albedo cumulative fraction H lt 18.0

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where(ap_H<18)]),bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction



xlim_min,xlim_max = bins[0], 0.0
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative fraction'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_18_0'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.pdf'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)



#albedo cumulative H lt 16.4 and 0.1 < pv < 0.3


ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where((ap_H<16.4) &(ap_albedo>0.1) & (ap_albedo<0.3))]),bins)

albedo_fraction = ap_albedo[np.where((ap_H<16.4) &(ap_albedo>0.1) & (ap_albedo<0.3))].shape[0]*1.0/ ap_albedo[np.where(ap_H<16.4)].shape[0]

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.3*albedo_fraction



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_16_4_albedo_0_1_to_0_3'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#albedo cumulative H lt 15.6 and 0.1 < pv < 0.3

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where((ap_H<15.6) &(ap_albedo>0.1) & (ap_albedo<0.3))]),bins)

albedo_fraction = ap_albedo[np.where((ap_H<15.6) &(ap_albedo>0.1) & (ap_albedo<0.3))].shape[0]*1.0/ ap_albedo[np.where(ap_H<15.6)].shape[0]

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.1*albedo_fraction



xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_15_6_albedo_0_1_to_0_3'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)

#albedo cumulative H lt 17.2 and 0.1 < pv < 0.3

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

a_hist_bins_min = -2.
a_hist_bins_max = 0.1
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_hist=make_histogram1d(np.log10(ap_albedo[np.where((ap_H<17.2) &(ap_albedo>0.1) & (ap_albedo<0.3))]),bins)

albedo_fraction = ap_albedo[np.where((ap_H<17.2) &(ap_albedo>0.1) & (ap_albedo<0.3))].shape[0]*1.0/ ap_albedo[np.where(ap_H<17.2)].shape[0]

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)
number_n_hist_fraction = n_hist_fraction*0.9*albedo_fraction

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.log10(0.2)#ap albedo

x_label = 'Log Albedo'
y_label = 'Cumulative number, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_albedo_cumulative_number_distribution_H_lt_17_2_albedo_0_1_to_0_3'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(number_n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_2(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#q distribution

#albedo cumulative H lt 17.2 and 0.1 < pv < 0.3

ap_a_au, ap_e, ap_i, ap_H, ap_albedo = np.loadtxt('morby_synthetic-Vatiras.dat').T

ap_q_au  = (1-ap_e) * ap_a_au

k_pen, q_pen = 1.4, 0.69
ap_penalty_function = k_pen * (q_pen - ap_q_au)


a_hist_bins_min = 0.05
a_hist_bins_max = 0.75
delta_a_hist_bins = 0.05
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

q_synth = np.linspace(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, 5000)

standard_penalty_function = k_pen * (q_pen - q_synth)

n_hist=make_histogram1d(ap_q_au,bins)

#plt.figure()
#plt.plot(bins,n_hist,'.')

n_hist_fraction = (1.*n_hist)/n_hist.sum()

#number_n_hist_fraction = n_hist_fraction*((ap_albedo[np.where(ap_H<18.)].shape[0] * 1.0)/1000.)

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=0.4571508134655605

x_label = 'q (au)'
y_label = 'Cumulative fraction, per bin fraction density'
file_name='morbidelli_aylochax_poswaamay_q_au_cumulative_number'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.png'

data_sets = np.array([np.cumsum(n_hist_fraction)])
make_hist_one_datasets_no_legend_with_vline_3(xtick_locs, data_sets, bins + (np.diff(bins)[0]/2), x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#New twilight survey pointing plot using only September 2019 to January 2020 pointings and the quads from Frank
'''
All_twilight_obs_quads
   expid                qcomment                       obsdate                obsjd            ra0            dec0       airmass    sunalt    field   fid   rcid 
 147110386   Partnership_Twilight              2021-01-11 02:29:34.429   2459225.6038657   327.84382593     3.16680489     2.212   -18.6384     494     2     31
 147110386   Partnership_Twilight              2021-01-11 02:29:34.429   2459225.6038657   330.70461598     6.99890561     2.212   -18.6384     494     2     54
 147110386   Partnership_Twilight              2021-01-11 02:29:34.429   2459225.6038657    329.7100686     5.95354779     2.212   -18.6384     494     2     40


'''

#twilight survey fields new survey pointing plot
#fields

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads.txt', usecols=(9,5,6,4,),skiprows=1)[np.where((all_field_mjd>58742)&(all_field_mjd<58880.0))].T

only_new_field_mjd -=2400000.5

fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 10

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd[j]
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]/64., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads
#im = plt.imshow(n_grid[:,:]/64., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads
#im = plt.imshow(n_grid[::-1,:]/64., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('vatira_survey_only_new_survey.pdf')


#twilight survey fields Sun centric distance quad version solar elongation
#fields

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads.txt', usecols=(9,5,6,4,),skiprows=1)[np.where((all_field_mjd>58742)&(all_field_mjd<58880.0))].T

only_new_field_mjd -=2400000.5

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        #if j % 100 ==0.0:
        #    print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        #print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs
vline_loc2=39.5413#elongation of 2020 AV2 on 2020 Jan 4

x_label = 'Sun-centric distance (deg)'
y_label = 'Cumulative fraction'
file_name='cumulative_sun_centric_distance_only_new_survey'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.pdf'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_two_vlines(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,vline_loc2,xlim_min,xlim_max)


#twilight survey fields Sun centric distance quad version solar elongation
#fields using latest fields since 2021 January 1

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads_2021.04.12.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads.txt', usecols=(9,5,6,4,),skiprows=1)[np.where(all_field_mjd>59215.0)].T

only_new_field_mjd -=2400000.5

only_new_field_mjd_decimal, only_new_field_mjd_day = np.modf(only_new_field_mjd)

only_new_field_mjd_day_unique = np.unique(only_new_field_mjd_day)

plt.ion()
eta = 23.439166666666665
#set up hist params
fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

a_hist_bins_min = 30.
a_hist_bins_max = 65.0
delta_a_hist_bins = 1.0
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins

n_grid = np.zeros(360*181).reshape(181,360)
n_hist = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))
n_hist_fraction = np.zeros(len(only_new_field_mjd_day_unique) *len(bins)).reshape(len(only_new_field_mjd_day_unique),len(bins))

rainterval = decinterval = 40

for jj in range(0, len(only_new_field_mjd_day_unique),1):
    if jj % 10 ==0.0:
        print "Unique day " + str(jj) +" out of " + str(len(only_new_field_mjd_day_unique))

    in_field_mjd = only_new_field_mjd_day[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_ra_deg = only_new_field_ra_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    in_field_dec_deg = only_new_field_dec_deg[np.where(only_new_field_mjd_day==only_new_field_mjd_day_unique[jj])]
    solar_dist_storage = np.zeros(len(in_field_mjd))
    for j in range(0, len(in_field_mjd),1):
        #if j % 100 ==0.0:
        #    print "In field " + str(j) +" out of " + str(len(in_field_ra_deg))
        pointing_date = in_field_mjd[j]
        pointing_ra_rad = np.radians(in_field_ra_deg[j])
        pointing_dec_rad = np.radians(in_field_dec_deg[j])
        pointing_d_ra_rad = np.radians(fov_size)/2.0
        pointing_d_dec_rad = np.radians(fov_size)/2.0

        lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
        #Generate eq to ec rotation matrix.
        eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
        #Generate Earth's geocenter heliocentric coords.
        xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
        #Equatorial to ecliptic coordinates for positional vectors only.
        xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
        xyz_Sol_ec_au = xyz_Earth_ec_au * -1
        #get spherical coords of sun towards earth
        long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
        long_opp_ec_rad = long_sol_ec_rad
        lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
        field_ra_rad = np.linspace(pointing_ra_rad-pointing_d_ra_rad,pointing_ra_rad+pointing_d_ra_rad, rainterval+1)
        ra_rad_offset = np.diff(field_ra_rad)[0]/2
        if pointing_ra_rad != 0.0:
            for q in range(0, len(field_ra_rad)):
                field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
                field_ra_rad += ra_rad_offset

        field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
        dec_rad_offset = np.diff(field_dec_rad)[0]/2
        field_dec_rad += dec_rad_offset

        field_ra_rad_median = np.median(field_ra_rad)
        field_dec_rad_median = np.median(field_dec_rad)

        field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad_median,field_dec_rad_median,pointing_date)
        if field_lon_rad < 0:
            field_lon_rad += 360
        oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
        oppos = sla.sla_ranorm(oppos)
        latcen = field_lat_rad - lat_opp_ec_rad
        y_ceil = int(np.ceil(np.degrees(latcen)))
        x_ceil = int(np.ceil(np.degrees(oppos)))-180-4
        n_grid[y_ceil][x_ceil] += 1.0/(rainterval**2)
        Sun_distance_deg = np.sqrt((np.abs(y_ceil)-90.)**2 + (np.abs(x_ceil)-180)**2)
        #print x_ceil, y_ceil, Sun_distance_deg
        solar_dist_storage[j]= Sun_distance_deg

    n_hist[jj,:]= make_histogram1d(solar_dist_storage,bins)


n_hist_sum = n_hist.sum(1)

n_hist_sum[np.where(n_hist_sum==0)]=1.0

for i in range(0,len(n_hist_sum)):
    n_hist_fraction[i] = n_hist[i]/n_hist_sum[i]

xlim_min,xlim_max = bins[0], bins[-1]
vline_loc=np.degrees(np.arcsin(0.718/1.0))#max elongation of IVAs
vline_loc2=39.5413#elongation of 2020 AV2 on 2020 Jan 4

x_label = 'Sun-centric distance (deg)'
y_label = 'Cumulative fraction'
file_name='cumulative_sun_centric_distance_only_2021_jan_1_to_2021_Apr_12'
xtick_locs = bins
notes='blah'
plt_dir = '/Users/bolin/NEO/Follow_up/APO_observing/'
ext='.pdf'

data_sets = np.array([np.cumsum(n_hist_fraction.mean(0))])
make_hist_one_datasets_no_legend_with_two_vlines(xtick_locs, data_sets, bins, x_label, y_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,vline_loc2,xlim_min,xlim_max)


#limiting magnitude vs time plots for twilight survey

lim_mag_date_JD, lim_mag_diff_mag_r = np.loadtxt('TwilightSurvMagLims_date_time_JD_airmass_diff_mag_lim_sci_mag_lim.txt',usecols=(2,4)).T

lim_mag_date_MJD = lim_mag_date_JD-2400000.5

time_start = sla.sla_cldj(2019,9,18)[0]
time_end = sla.sla_cldj(2020,2,1)[0]

plt.figure()
plt.hist(lim_mag_diff_mag_r[np.where((lim_mag_date_MJD>time_start)&(lim_mag_date_MJD<time_end)&(lim_mag_diff_mag_r>15.5))],30)



a_hist_bins_min = 18.0
a_hist_bins_max = 21.75
delta_a_hist_bins = 0.25
a_hist_bins = np.arange(a_hist_bins_min, a_hist_bins_max + delta_a_hist_bins, delta_a_hist_bins)[1:] - (delta_a_hist_bins/1.0)
bins = a_hist_bins
vline_loc = 18.29 #assumes 0.275 colors for S type
xlim_min,xlim_max = 17.75,21.0

x_label = 'V magnitude'
file_name='V_lim_mag'
xtick_locs = a_hist_bins

#set to our a datasets
first_hist_values = make_histogram1d(lim_mag_diff_mag_r[np.where((lim_mag_date_MJD>time_start)&(lim_mag_date_MJD<time_end)&(lim_mag_diff_mag_r>15.5))]+0.3,bins)
#second_hist_values = make_histogram1d(vat_det_H,bins)

data_sets = np.array([first_hist_values])
make_hist_one_dataset_no_legend(xtick_locs[::2], data_sets, bins, x_label, notes, plt_dir, file_name, ext,fontsize_standard,paperwidth,margin,paperheight,vline_loc,xlim_min,xlim_max)


#Q distribution of vatiras

vat_all_Q = vat_all_a_au * (1 + vat_all_e)
print len(np.where(vat_all_Q<0.55)[0])

#6% have Q < 0.55 au

print (len(np.where(vat_all_Q<0.55)[0])*1.0)/len(vat_all_Q)

#collision statistics

'''
('Particles [3, 7] collided', 24816174.754363425, ' years')

'''


#earlier than 25 Myrs
collider_early, collide_time_early = np.loadtxt('AV2_collisions_earlier_than_25_myrs').T

print len(np.where(collider_early==0)[0])*1.0/len(collider_early),len(np.where(collider_early==1)[0])*1.0/len(collider_early),len(np.where(collider_early==2)[0])*1.0/len(collider_early),len(np.where(collider_early==3)[0])*1.0/len(collider_early),len(np.where(collider_early==4)[0])*1.0/len(collider_early)


#later than 25 Myrs
collider_late, collide_time_late = np.loadtxt('AV2_collisions_later_than_25_myrs').T

print len(np.where(collider_late==0)[0])*1.0/len(collider_late),len(np.where(collider_late==1)[0])*1.0/len(collider_late),len(np.where(collider_late==2)[0])*1.0/len(collider_late),len(np.where(collider_late==3)[0])*1.0/len(collider_late),len(np.where(collider_late==4)[0])*1.0/len(collider_late)

#total early + late.

print (len(np.where(collider_early==0)[0])+len(np.where(collider_late==0)[0]))*1.0/(len(collider_late)+len(collider_early)),(len(np.where(collider_early==1)[0])+len(np.where(collider_late==1)[0]))*1.0/(len(collider_late)+len(collider_early)),(len(np.where(collider_early==2)[0])+len(np.where(collider_late==2)[0]))*1.0/(len(collider_late)+len(collider_early)),(len(np.where(collider_early==3)[0])+len(np.where(collider_late==3)[0]))*1.0/(len(collider_late)+len(collider_early)),(len(np.where(collider_early==4)[0])+len(np.where(collider_late==4)[0]))*1.0/(len(collider_late)+len(collider_early))


#no collisions

collide_time_no = np.loadtxt('AV2_no_collision').T

#total no collide + early collide sims sims

len(collide_time_no)  + len(collide_time_early)


#weight early + late by number of no collisions sims

collid_sun_no = (len(np.where(collider_late==0)[0])*1.0/len(collider_late)) * len(collide_time_no[7:])#dont include 50+ my integrations, assume they survive
collid_mercury_no = (len(np.where(collider_late==1)[0])*1.0/len(collider_late)) * len(collide_time_no[7:])
collid_venus_no = (len(np.where(collider_late==2)[0])*1.0/len(collider_late)) * len(collide_time_no[7:])
collid_earth_no = (len(np.where(collider_late==3)[0])*1.0/len(collider_late)) * len(collide_time_no[7:])
collid_mars_no = (len(np.where(collider_late==4)[0])*1.0/len(collider_late)) * len(collide_time_no[7:])


collide_sun = collid_sun_no + len(np.where(collider_early==0)[0])
collide_mercury = collid_mercury_no + len(np.where(collider_early==1)[0])
collide_venus = collid_venus_no + len(np.where(collider_early==2)[0])
collide_earth = collid_earth_no + len(np.where(collider_early==3)[0])
collide_mars = collid_mars_no + len(np.where(collider_early==4)[0])

print collide_sun/(len(collide_time_no)  + len(collide_time_early)), collide_mercury/(len(collide_time_no)  + len(collide_time_early)), collide_venus/(len(collide_time_no)  + len(collide_time_early)), collide_earth/(len(collide_time_no)  + len(collide_time_early)), collide_mars/(len(collide_time_no)  + len(collide_time_early))

fraction_survive = 1- np.sum([collide_sun/(len(collide_time_no)  + len(collide_time_early)), collide_mercury/(len(collide_time_no)  + len(collide_time_early)), collide_venus/(len(collide_time_no)  + len(collide_time_early)), collide_earth/(len(collide_time_no)  + len(collide_time_early)), collide_mars/(len(collide_time_no)  + len(collide_time_early))])

#collisional lifetime

print np.median(np.concatenate([collide_time_late,collide_time_early]))

time_all_clones_collide = np.concatenate([collide_time_late,collide_time_early])

#Q 20 mil

import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)
times = np.concatenate([times_backward,times_forward])
times_year = np.array([times/year])[0]/1e6

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/sims_plots/'

P_jacob_backwards = np.load(save_directory+'P_jacob_backwards.pyc')
P_jacob_forwards = np.load(save_directory+'P_jacob_forwards.pyc')
P_jacob = np.concatenate([P_jacob_backwards,P_jacob_forwards])

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards,a_au_forwards])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e = np.concatenate([e_backwards,e_forwards])

q_20_mil_au, Q_20_mil_au = a_au*(1-e),a_au*(1+e)

years_ivo_20_mil = len(np.where(Q_20_mil_au[10000:]<0.718)[0])* np.abs(np.diff(times_year)[0])

'''
In [79]: years_ivo_20_mil
Out[79]: 384000.0

'''


#Q 60 mil

import rebound
import datetime

%matplotlib osx

year = 2 * np.pi

num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward = np.linspace(0.,num_years*year, Noutputs)

import numpy as np
year = 2.*np.pi # One year in units where G=1
num_years = -10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = -20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_backward_2 = np.linspace(0.,num_years*year, Noutputs) + times[-1]

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward = np.linspace(0.,num_years*year, Noutputs)

num_years = 10000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times = np.linspace(0.,num_years*year, Noutputs)
num_years = 20000000.
outputs_per_year = 0.001
Noutputs = int(outputs_per_year * np.abs(num_years))#you want roughly 1000 times per year
times_forward_2 = np.linspace(0.,num_years*year, Noutputs) + times[-1]


times = np.concatenate([times_backward_2,times_backward,times_forward,times_forward_2])
times_year = np.array([times/year])[0]/1e6

save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_10mil/'
save_directory2 = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/'

plot_save_directory = '/Users/bolin/NEO/Follow_up/APO_observing/2020AV2_sims/first_run_30mil/sims_plots/'

a_au_backwards = np.load(save_directory+'a_au_backwards.pyc')
a_au_forwards = np.load(save_directory+'a_au_forwards.pyc')
a_au_backwards2 = np.load(save_directory2+'a_au_backwards.pyc')
a_au_forwards2 = np.load(save_directory2+'a_au_forwards.pyc')
a_au = np.concatenate([a_au_backwards2,a_au_backwards,a_au_forwards,a_au_forwards2])


e_backwards = np.load(save_directory+'e_backwards.pyc')
e_forwards = np.load(save_directory+'e_forwards.pyc')
e_backwards2 = np.load(save_directory2+'e_backwards.pyc')
e_forwards2 = np.load(save_directory2+'e_forwards.pyc')
e = np.concatenate([e_backwards2,e_backwards,e_forwards,e_forwards2])

q_60_mil_au, Q_60_mil_au = a_au*(1-e),a_au*(1+e)

years_ivo_60_mil = len(np.where(Q_60_mil_au[30000:]<0.718)[0])* np.abs(np.diff(times_year)[0])

'''
In [76]: years_ivo_60_mil
Out[76]: 432800.0

'''


#Sun-centric plot for quads for 2021-04-04 UTC with long slews only

#twilight survey fields new survey pointing plot
#fields

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads_2021.04.12.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads_2021.04.12.txt', usecols=(9,5,6,4,),skiprows=1)[np.where((all_field_mjd>59308)&(all_field_mjd<59309.0))].T

only_new_field_mjd -=2400000.5

fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 10

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd[j]
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)



fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]/1., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads, and flip with ::-1,::-1 because the new list has objects in reverse date order
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('twilight_survey_2021_Apr_04.pdf')


'''
IVO HG

24 November 2020

17.6 - phaseFunction ( np.radians(109.8212),0.25, 0.549031301259, 0.65521020970240, 'HG') + .47

#using G = 0.15
In [20]: 17.6 - phaseFunction ( np.radians(109.8212),0.15, 0.549031301259, 0.65521020970240, 'HG') + .47
Out[20]: 16.13573081412584


#Using G = 0.2
17.6 - phaseFunction ( np.radians(109.8212),0.2, 0.549031301259, 0.65521020970240, 'HG') + .47
Out[28]: 16.234665023788345

0.1 magnitudes fainter

26 November 2020
18.0 - phaseFunction ( np.radians(107.0503),0.25, 0.540755315762, 0.68207980278854, 'HG') + .47


16.838598094326077

#uncertainty caused by G

In [653]:  17.6 - phaseFunction ( np.radians(109.8212),0.13, 0.549031301259, 0.65521020970240, 'HG') + .47
Out[653]: 16.093486820564433

In [654]:  17.6 - phaseFunction ( np.radians(109.8212),0.35, 0.549031301259, 0.65521020970240, 'HG') + .47
Out[654]: 16.486694135943786


#H mag errors
~.2

print np.sqrt(.8**2 + .2**2)

#H mag errors ~0.8
'''


'''
Fields adjustment, to make Twilight survey fields more compact for the high fields

Field generator code, e.g. python select_field_ra_dec_double_TS_L_TS_M_separate.py 59241 m 8 5 27 0 5 5 17 0 

'''

all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads_2021.05.10.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

#date input
date_to_check_low_mjd = sla.sla_cldj(2021,02,05)[0]
date_to_check_high_mjd = date_to_check_low_mjd+1

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = all_field_id[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_ra_deg[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_dec_deg[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_mjd[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))]

fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 10

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd[j]
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]/1., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads, and flip with ::-1,::-1 because the new list has objects in reverse date order
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('twilight_survey_2021_Feb_5.pdf')

#ZTF phase II fields


all_field_id, all_field_ra_deg, all_field_dec_deg, all_field_jd = np.loadtxt('/Users/bolin/NEO/Follow_up/APO_observing/All_twilight_obs_quads_2021.05.10.txt', usecols=(9,5,6,4,),skiprows=1).T

all_field_mjd = all_field_jd-2400000.5

#date input
date_to_check_low_mjd = sla.sla_cldj(2021,01,01)[0]
date_to_check_high_mjd = date_to_check_low_mjd+180

only_new_field_id, only_new_field_ra_deg, only_new_field_dec_deg, only_new_field_mjd = all_field_id[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_ra_deg[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_dec_deg[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))], all_field_mjd[np.where((all_field_mjd>date_to_check_low_mjd)&(all_field_mjd<date_to_check_high_mjd))]

fov_size = (3.7081 * 2)/16.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 10

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd[j]
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]/1., extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2')) #need to divide by 64 since we are counting quads, and flip with ::-1,::-1 because the new list has objects in reverse date order
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('twilight_survey_2021_Jan_1_to_May_10.pdf')




#synthetic fields
'''

#for 2021 April 12
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 e 5 5 17 0 4 5 14 5 > field_test_2021_Apr_12
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 m 5 5 17 0 5 5 17 5 >> field_test_2021_Apr_12

#for 2021 May 10
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 e 5 5 17 0 3 5 11 5 > field_test_2021_May_10
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 m 5 5 17 0 5 5 17 5 >> field_test_2021_May_10

'''

#2021 April 12

only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_Apr_12',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,04,12)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_Apr_12.pdf')

#2021 May 10
only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_May_10',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,05,10)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_May_10.pdf')


#tight mode

'''

#for 2021 April 12
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 e 5 5 17 0 4 5 14 0 > field_test_2021_Apr_12_tight
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 m 5 5 17 0 5 5 17 0 >> field_test_2021_Apr_12_tight

#for 2021 May 10
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 e 5 5 17 0 3 5 11 0 > field_test_2021_May_10_tight
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 m 5 5 17 0 5 5 17 0 >> field_test_2021_May_10_tight

'''

only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_Apr_12_tight',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,04,12)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_Apr_12_tight.pdf')

#2021 May 10
only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_May_10_tight',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,05,10)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_May_10_tight.pdf')

#tight less repeats

'''

#for 2021 April 12
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 e 5 5 17 0 4 5 14 0 > field_test_2021_Apr_12_tight_less_repeats
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59316 m 5 5 17 0 5 5 17 2 >> field_test_2021_Apr_12_tight_less_repeats

#for 2021 May 10
#evening
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 e 5 5 17 0 3 5 11 0 > field_test_2021_May_10_tight_less_repeats
#morning
python select_field_ra_dec_double_TS_L_TS_M_separate_more_compact_high_fields.py 59344 m 5 5 17 0 5 5 17 2 >> field_test_2021_May_10_tight_less_repeats

'''

only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_Apr_12_tight_less_repeats',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,04,12)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_Apr_12_tight_less_repeats.pdf')

#2021 May 10
only_new_field_ra_deg, only_new_field_dec_deg = np.loadtxt('field_test_2021_May_10_tight_less_repeats',usecols=(1,2,)).T

only_new_field_mjd = sla.sla_cldj(2021,05,10)[0]

fov_size = (3.7081 * 2)/1.#because only doing individual ccd quads

plt.ion()
extent = [180.0, -180.0, -90.0, 90.0]
eta = 23.439166666666665
n_grid = np.zeros(360*181).reshape(181,360)
rainterval = decinterval = 40

for j in range(0, len(only_new_field_ra_deg),1):
    if j % 100 ==0.0:
        print str(j) +" out of " + str(len(only_new_field_ra_deg))
    pointing_date = only_new_field_mjd
    pointing_ra_rad = np.radians(only_new_field_ra_deg[j])
    pointing_dec_rad = np.radians(only_new_field_dec_deg[j])
    pointing_d_ra_rad = np.radians(fov_size)/2.0
    pointing_d_dec_rad = np.radians(fov_size)/2.0

    lam, bet = sla.sla_ecleq(pointing_ra_rad, pointing_dec_rad,pointing_date)
    #Generate eq to ec rotation matrix.
    eq2ec_rmat_mJ2000 = sla.sla_ecmat(pointing_date)
    #Generate Earth's geocenter heliocentric coords.
    xyz_Earth_eq_au, dxdydz_Earth_eq_au_d, xyz_barycenter_au, dxdydz_barycenter_au_d = sla.sla_epv( pointing_date ) #returns velocity vectors in au_d
    #Equatorial to ecliptic coordinates for positional vectors only.
    xyz_Earth_ec_au  = sla.sla_dmxv( eq2ec_rmat_mJ2000, xyz_Earth_eq_au )
    xyz_Sol_ec_au = xyz_Earth_ec_au * -1
    #get spherical coords of sun towards earth
    long_sol_ec_rad, lat_sol_ec_rad, cdist, cdlon,cdlat,cdr = sla.sla_dc62s( np.concatenate((xyz_Sol_ec_au,np.array([0.0,0.0,0.0]))))
    long_opp_ec_rad = long_sol_ec_rad
    lat_opp_ec_rad = np.pi - (lat_sol_ec_rad + (np.pi/2))
    field_ra_rad = np.linspace(pointing_ra_rad-(pointing_d_ra_rad/np.cos(pointing_dec_rad)),pointing_ra_rad+(pointing_d_ra_rad/np.cos(pointing_dec_rad)), rainterval+1)
    ra_rad_offset = np.diff(field_ra_rad)[0]/2
    if pointing_ra_rad != 0.0:
        for q in range(0, len(field_ra_rad)):
            field_ra_rad[q] = sla.sla_ranorm(field_ra_rad[q])
            field_ra_rad += ra_rad_offset

    field_dec_rad = np.linspace(pointing_dec_rad-pointing_d_dec_rad,pointing_dec_rad+pointing_d_dec_rad, decinterval+1)
    dec_rad_offset = np.diff(field_dec_rad)[0]/2
    field_dec_rad += dec_rad_offset
    for k in range(0,rainterval):
        for l in range (0, decinterval):
            field_lon_rad, field_lat_rad = sla.sla_eqecl(field_ra_rad[k],field_dec_rad[l],pointing_date)
            if field_lon_rad < 0:
                field_lon_rad += 360
            oppos = (field_lon_rad -long_opp_ec_rad) + 2*np.pi
            oppos = sla.sla_ranorm(oppos)
            latcen = field_lat_rad - lat_opp_ec_rad
            y_ceil = np.ceil(np.degrees(latcen))
            x_ceil = np.ceil(np.degrees(oppos))
            n_grid[int(y_ceil)][int(x_ceil)-180-4] += 1.0/(rainterval**2)

fig = plt.figure(figsize=(24,10))
im = plt.imshow(n_grid[::-1,::-1]*decinterval*1.0, extent = [180.0, -180.0, -90.0, 90.0], cmap=cm.get_cmap('gnuplot2'))
plt.xlabel("Sun-centric ecliptic longitude (deg)",fontsize="30")
plt.ylabel("Sun-centric ecliptic latitude (deg)",fontsize="30")
plt.tick_params(axis='both', which='major', labelsize=30)
ax = plt.gca();
ax.grid(color='w', linewidth=1)
plt.xlim(70,-70)
plt.ylim(-20,60)
ax.set_yticks(range(-20, 80, 20))

#divider
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="2.5%", pad=0.25)

#cbar fontsize change
cb = plt.colorbar(im,cax=cax)
for t in cb.ax.get_yticklabels():
     t.set_fontsize(30)

cb.set_label('Number of visits',fontsize="30")
cb.ax.tick_params(labelsize=23)

plt.savefig('synthetic_twilight_survey_2021_May_10_tight_less_repeats.pdf')


'''
SOAR/Goodman recovery data from 2021 July 17 taken by Kevin Hardegree-Ullman

'''

#basic acronym reduction



#dont truncate data frame information
pd.set_option('display.max_colwidth', -1)

# ignore overwriting reduced files warnings in case you need to rerun
warnings.filterwarnings('ignore', message='Overwriting existing file')


# take directory from user or assume current directory
if len(sys.argv) > 1:
    direc = sys.argv[1]
else:
    direc = '.'

cals_direc = os.path.join(direc, 'reduced', 'cals')
reduced_direc = os.path.join(direc, 'reduced', 'data')

# directories for reduced images
if not os.path.exists(cals_direc):
    os.makedirs(cals_direc)
if not os.path.exists(reduced_direc):
    os.makedirs(reduced_direc)


# grab all files from the directory; organize dataframe
files = glob.glob(os.path.join(direc, "*.fits"))

df = pd.DataFrame(files,columns=['fname'])
df['filt'] = pd.Series("", index=df.index)
df['exp'] = pd.Series("", index=df.index)
df['objname'] = pd.Series("", index=df.index)

for ff,fname in enumerate(files):
    try:
        df['filt'][ff] = pyfits.open(fname)[0].header['filter']
        df['exp'][ff] = pyfits.open(fname)[0].header['EXPTIME']
        df['objname'][ff] = pyfits.open(fname)[0].header['OBJECT']
    except IOError:
        print('\n File corrupt or missing: ' + fname)


def trim_image(f):
    """
    trim_image returns a trimmed version of the raw image. The ARCTIC detector is structured in four quadrants which can be read out individually (Quad Mode) or as a whole (Lower Left Mode) and trim_image identifies which readout mode was used and crops the image accordingly.
    Parameters
    ----------
    f : raw fits image from ARCTIC
    overscan_poly_order : order of polynomial used to fit overscan
    Returns
    -------
    alldat : a list with [the image in a numpy array, the astropy header]
    https://bitbucket.org/bbolin/observing/
    """
    datfile = pyfits.getdata(f, header=True)
    dat_raw = datfile[0]
    dat_head = datfile[1]
    alldat = [dat_raw,dat_head]
    return alldat


### CREATE MASTER BIAS #######################################

print('\n >>> Starting bias combine...')

bias_idx = df[df['objname'] == 'bias_2x2'].index.tolist()
if len(bias_idx) == 0:
    print('   > No biases found. Continuing reductions...')
    bias=0.
else:
    biases = np.array([trim_image(df['fname'][n])[0] for n in bias_idx])
    bias = np.median(biases,axis=0)
    pyfits.writeto(os.path.join(cals_direc, 'master_bias.fits'),bias.astype(np.float32),overwrite=True)
    print('   > Created master bias')





### CREATE MASTER FLATS ######################################
### these are bias and dark subtracted then normalized

# array of all filters found
filters = list(filter(None,pd.unique(df.filt.ravel())))

print('\n >>> Starting flats...')

for ii in range(0,len(filters)):

    flat_idx = df[(df['filt'] == filters[ii]) & ((df['objname'] == 'domeflat_r_2x2') | (df['objname'] == 'flat_r') | (df['objname'] == 'flat_i') | (df['objname'] == 'flat_z'))].index.tolist()

    if len(flat_idx) == 0:
        print('   > No flats found for the ' + str(filters[ii]) + ' filter. Continuing reductions...')

    flats = np.array([trim_image(df['fname'][n])[0] for n in flat_idx]) - bias

    flat_final = np.median(flats,axis=0)
    flat_final /= np.max(flat_final)

    name = os.path.join(cals_direc, 'master_flat_{0}.fits'.format(filters[ii]))
    #name = os.path.join(cals_direc, 'master_flat_i.fits'.format(filts))
    pyfits.writeto(name,flat_final,overwrite=True)
    print('   > Created master '+ str(filters[ii])+' flat')



### REDUCE SCIENCE IMAGES ####################################
# (science_raw - bias) / masterflat

dat_idx = df[(df['objname'] == '2020_AV2')].index.tolist()

print('\n >>> '+str(len(dat_idx))+' science images found. Starting reductions...')
for n in dat_idx:
    datfile = trim_image(df['fname'][n])
    dat_raw = datfile[0]
    dat_head = datfile[1]

    filt = df['filt'][n]
    print (filt)
    try:
        flat = flat = pyfits.getdata(os.path.join(cals_direc,'master_flat_{0}.fits'.format(filt)))
    except IOError:
        print('   > Warning! No ' + str(df['filt'][n]) + ' filter flat found for ' + df['fname'][n])
        flat = 1.
    dat = (dat_raw - bias) / flat
    name = os.path.join(reduced_direc,'reduced_{0}'.format(os.path.basename(df['fname'][n])))
    pyfits.writeto(name,dat[300:1200,300:1200].astype(np.float32),overwrite=True,header=dat_head)

print('\n >>> Finished reductions!!!! \n')



'''
Magellan/Goodman recovery data from 2021 July 17 taken by Kevin Hardegree-Ullman

'''


#convert hours minutes seconds to fractional days

hours_minutes_seconds_to_frac_day(np.asarray('10:59:27'.split(':')).astype('float'))+ ((15.)/(24.*3600.))


#source region probability for 2021 PH27

#H, a, e, i, Pnu6, P5:2, P2:1, Phu,  P3:1,Ppho, Pjfc
#17.73   0.461640  0.7102117   31.78163   0.76160002 0.000000 0.0000000  0.0371700  0.1921400  0.0090900  0.0000000